All Projects → krzkaczor → Babel Plugin Tailcall Optimization

krzkaczor / Babel Plugin Tailcall Optimization

Licence: mit
Tail call optimization for JavaScript!

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Babel Plugin Tailcall Optimization

Babel Plugin Transform React To Vue
Transform React component to Vue component (beta)
Stars: ✭ 157 (-8.72%)
Mutual labels:  babel
Babel 7 Typescript Example
Example TypeScript project built on top of new Babel 7 features. Includes Jest and Enzyme.
Stars: ✭ 166 (-3.49%)
Mutual labels:  babel
Graphpack
☄️ A minimalistic zero-config GraphQL server.
Stars: ✭ 1,994 (+1059.3%)
Mutual labels:  babel
Babel Polyfills
A set of Babel plugins that enable injecting different polyfills with different strategies in your compiled code.
Stars: ✭ 158 (-8.14%)
Mutual labels:  babel
React Ast
render abstract syntax trees with react
Stars: ✭ 160 (-6.98%)
Mutual labels:  babel
Gopablo
🐺 Static site generator.
Stars: ✭ 166 (-3.49%)
Mutual labels:  babel
Webpack Encore
A simple but powerful API for processing & compiling assets built around Webpack
Stars: ✭ 1,975 (+1048.26%)
Mutual labels:  babel
React Tradingview Widget
React component for rendering the TradingView Advanced Real-Time Chart Widget.
Stars: ✭ 170 (-1.16%)
Mutual labels:  babel
React Redux Universal Boilerplate
An Universal ReactJS/Redux Boilerplate
Stars: ✭ 165 (-4.07%)
Mutual labels:  babel
Lite Editor
A Modern WYSIWYG Editor especially for inline elements
Stars: ✭ 169 (-1.74%)
Mutual labels:  babel
Shopify Webpack Themekit
Shopify development tool using webpack and themekit
Stars: ✭ 157 (-8.72%)
Mutual labels:  babel
Reactn
React, but with built-in global state management.
Stars: ✭ 1,906 (+1008.14%)
Mutual labels:  babel
Hugo theme pickles
Modern, Simple and beautiful Hugo theme
Stars: ✭ 168 (-2.33%)
Mutual labels:  babel
Express Webpack React Redux Typescript Boilerplate
🎉 A full-stack boilerplate that using express with webpack, react and typescirpt!
Stars: ✭ 156 (-9.3%)
Mutual labels:  babel
React Blog
personal blog design by react
Stars: ✭ 170 (-1.16%)
Mutual labels:  babel
Babel Plugin Flow To Typescript
Babel plugin to convert Flow code into TypeScript
Stars: ✭ 156 (-9.3%)
Mutual labels:  babel
Getlibs
OBSOLETE, DO NOT USE: This project is no longer maintained
Stars: ✭ 166 (-3.49%)
Mutual labels:  babel
Param.macro
Partial application syntax and lambda parameters for JavaScript, inspired by Scala's `_` & Kotlin's `it`
Stars: ✭ 170 (-1.16%)
Mutual labels:  babel
Babel Plugin React Html Attrs
Babel plugin which transforms HTML and SVG attributes on JSX host elements into React-compatible attributes
Stars: ✭ 170 (-1.16%)
Mutual labels:  babel
Graphql Tag.macro
Babel Macro for graphql-tag
Stars: ✭ 168 (-2.33%)
Mutual labels:  babel

babel-plugin-tailcall-optimization

JavaScript Style Guide

Tail call optimization for JavaScript!

Installation

npm install babel-plugin-tailcall-optimization --save-dev

and add to your .babelrc:

"plugins": ["tailcall-optimization"]

if you use [email protected] use [email protected] package

How does it work?

We rewrite functions with tail calls to ones using while loops. Original function with tail call:

function counter (n, acc = 0) {
  if (n === 0) {
    return acc
  } else {
    return counter(n - 1, acc + 1)
  }
}

gets rewritten to this:

function counter(n, acc = 0) {
  var _repeat = true;

  var _n, _acc;

  while (_repeat) {
    _repeat = false;

    if (n === 0) {
      return acc;
    } else {
      _n = n - 1
      _acc = acc + 1
      n = _n
      acc = _acc
      _repeat = true;
      continue;
    }
  }
}

Plugin does not affect functions without TCOs so it's safe to use.

Benchmarks

For Fibonacci Sequence example benchmark.js results are:

Fibonacci Sequence without TCO x 270,170 ops/sec ±1.14% (85 runs sampled)
Fibonacci Sequence with TCO x 1,298,276 ops/sec ±1.24% (83 runs sampled)

So function after TCO optimization is almost 5 times faster.

Benchmark code

Known issues

  • Currently when plugin detects function creation within tailcalled function it does not optimize it. It's related to difficulties in implementation (function scoping rules). Read more: https://phabricator.babeljs.io/T6869

  • It does not work for mutual recursive functions. I guess it's not super big problem - even JVM does not do this.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].