Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/krzkaczor/babel-plugin-tailcall-optimization
Tail call optimization for JavaScript!
https://github.com/krzkaczor/babel-plugin-tailcall-optimization
babel javascript tail-call-optimization
Last synced: 6 days ago
JSON representation
Tail call optimization for JavaScript!
- Host: GitHub
- URL: https://github.com/krzkaczor/babel-plugin-tailcall-optimization
- Owner: krzkaczor
- License: mit
- Created: 2016-08-09T23:45:12.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-01-09T23:59:33.000Z (almost 3 years ago)
- Last Synced: 2024-10-23T08:27:13.842Z (13 days ago)
- Topics: babel, javascript, tail-call-optimization
- Language: JavaScript
- Size: 76.2 KB
- Stars: 193
- Watchers: 13
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# babel-plugin-tailcall-optimization
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)Tail call optimization for JavaScript!
## Installation
```bash
npm install babel-plugin-tailcall-optimization --save-dev
```and add to your `.babelrc`:
```js
"plugins": ["tailcall-optimization"]
```*if you use babel@6 use `babel-plugin-tailcall-optimization@1` package*
## How does it work?
We rewrite functions with tail calls to ones using while loops. Original function with tail call:
```js
function counter (n, acc = 0) {
if (n === 0) {
return acc
} else {
return counter(n - 1, acc + 1)
}
}
```gets rewritten to this:
```js
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](https://github.com/krzkaczor/babel-plugin-tailcall-optimization/blob/master/examples/fibonacciSeq.js) 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](https://github.com/krzkaczor/babel-plugin-tailcall-optimization/blob/master/benchmark/fibonacciSeq.js)
## 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.