• Stars
    star
    191
  • Rank 202,877 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tail call optimization for JavaScript!

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

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:

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.

More Repositories

1

TypeStrict

ESLint config focused on maximizing type safety 💪
JavaScript
220
star
2

ny

🗽 Fast, Proxy Package Manager for JavaScript
Rust
89
star
3

babel-plugin-proxy

Use ES6 proxies today!
JavaScript
65
star
4

hyperterm-paste

Makes pasting into hyperterm safe and easy
JavaScript
45
star
5

ts-generator

The missing piece for fully typesafe Typescript apps
TypeScript
30
star
6

awesome-optimism-bridges

List of currently developed optimism bridges
25
star
7

standardts

Opinionated code style for TypeScript
JavaScript
22
star
8

tadd

📺 Installs packages and automatically finds best Typescript typings
TypeScript
20
star
9

fuel-attack

TypeScript
18
star
10

SwiftScript

SwiftScript allows you to run your swift code directly in browser by translating it into JavaScript.
JavaScript
17
star
11

TypeSwift

Statically typed bridge between Swift and JavaScript using TypeScript definitions
TypeScript
15
star
12

psp-developer-guide

10
star
13

ts-boilerplate

TypeScript + ESLint + Prettier + TypeSTRICT
JavaScript
6
star
14

Chess.ai.js

Artificial intelligence for chess
JavaScript
6
star
15

SwiftAST

JavaScript
5
star
16

quake-on-unix

How to get Quake running on linux / mac
4
star
17

CSP-Solver

General solver for constraint satisfaction problems
C++
4
star
18

EthereumQL

TypeScript
3
star
19

cheatcalls-eip

TypeScript
3
star
20

truffle-codechecks

JavaScript
2
star
21

magic-container

Type safe dependency injection container with super powers!
TypeScript
2
star
22

CobolToCSharpTranslator

Cobol85 translator to CSharp written in JavaScript (ES6)
JavaScript
2
star
23

docker-devbox

dockerized ubuntu + common utils
Dockerfile
1
star
24

lighthouse-keeper-example

JavaScript
1
star
25

warsawjs-demo-backup

JavaScript
1
star
26

rust-fun

Rust
1
star
27

rusty-lisp

Simple LISP interpreter written in rust
Rust
1
star
28

CspQueensProblemGenerator

Simple generator of queens problem to CSP-DSL
Java
1
star
29

monorepo-boilerplate

JavaScript
1
star
30

Typechain-example

Example app using typechain
JavaScript
1
star
31

Warsaw-TS-meetup

Warsaw Typescript Meetup
1
star
32

geth-private-node

Easy to setup geth private node with lots of ether!
JavaScript
1
star
33

refined-etherscan

Adds additional features, aiding debugging to etherscan.com
HTML
1
star
34

synchronisify

Turns asynchronous constructor into synchronous one.
JavaScript
1
star