tailcall

0.0.0 • Public • Published

Build Status js-standard-style

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.

Example

Input (tail recursive factorial function):

function fact (n, acc) {
  acc = acc != null ? acc : 1
  if (< 2) return 1 * acc
  return fact(- 1, acc * n)
}

Output (no more recursive tail calls):

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 (< 2)
            return 1 * acc;
    }
}

Install

With npm do:

npm install tailcall

Usage via browserify

browserify -t tailcall index.js

or add it to your package.json:

{
  "browserify": {
    "transform": ["tailcall"]
  }
}

CLI

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.

License

Licensed under the ISC license. See LICENSE file for further info.

Package Sidebar

Install

npm i tailcall

Weekly Downloads

0

Version

0.0.0

License

ISC

Last publish

Collaborators

  • alexandergugel