Giter Club home page Giter Club logo

babel-plugin-tailcall-optimization's Introduction

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"]

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 releated to this bug in old babel implementation: 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.

babel-plugin-tailcall-optimization's People

Contributors

amilajack avatar krzkaczor avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.