Eliminate tail recursive function calls
npm install tailcall


tailcall is a browserify transform and command line utility that can be used for eliminating tail calls in recursive functions (TCO = tail call optimization). This prevents excessive growth of the used call stack and generally speaking increases performance (in most cases).
Tail call optimization is are part of the ECMAScript 6 spec:
Tail call optimization in ECMAScript 6
tailcall uses acorn to generate and traverse the AST.
Input (tail recursive factorial function):
``js`
function fact (n, acc) {
acc = acc != null ? acc : 1
if (n < 2) return 1 * acc
return fact(n - 1, acc * n)
}
Output (no more recursive tail calls):
`js`
function fact(n, acc) {
var __n, __acc, __;
while (true) {
acc = acc != null ? acc : 1;
__n = n - 1;
__acc = acc * n;
n = __n;
acc = __acc;
if (n < 2)
return 1 * acc;
}
}
With npm do:
``
npm install tailcall
``
browserify -t tailcall index.js
or add it to your package.json:
`json`
{
"browserify": {
"transform": ["tailcall"]
}
}
Usage of the command line utility usually requires a global install (via npm i -g tailcall) or a npm script.
``
tailcall index.js
`
usage:
tailcall file
Eliminate tail recursive function calls from file, printing the
transformed file contents to stdout.
tailcall
tailcall -
Eliminate tail recursive function calls from file, printing the`
transformed file contents to stdout.
Licensed under the ISC license. See LICENSE` file for further info.