Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-tailcall-optimization

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-tailcall-optimization

Tail call optimization for JavaScript!

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

babel-plugin-tailcall-optimization

JavaScript Style Guide

Tail call optimization for JavaScript!

Installation

*if you use babel@6 use babel-plugin-tailcall-optimization@1 package.

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 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.

FAQs

Package last updated on 15 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc