Skip to content

A babel plugin for performing tail call optimization in recursive functions

Notifications You must be signed in to change notification settings

ontanj/babel-plugin-recursive-tail-calls

Repository files navigation

Recursive tail calls

A babel plugin for performing tail call optimization in recursive functions.

Usage

Download the plugin from npm, using e.g. npm or yarn.

npm i babel-plugin-recursive-tail-calls

Add it to the plugins in babel.config.json.

More information on using Babel here.

Rationale

Tail call optimization was included in the ECMAScript 2015 language specification. However, some teams raised concerns about this feature, and therefore it is so far only supported by Safari.

In functional programming, recursion is the main way to simulate a loop. So for those wanting to take a functional approach to JavaScript, some kind of tail call optimization is needed to avoid exceeding the maximum stack size.

Implementation

This plugin does not implement the feature as mentioned in the language specification. Rather, it aims to find all locations where a function calls itself in tail position, and rewrites those functions using a loop.

// before transpilation
export function f(a) {
  if (a <= 0) {
    return a;
  }
  return f(a - 1);
}

// after transpilation
export function f(a) {
  _tailCallLoop: while (true) {
    if (a <= 0) {
      return a;
    }
    [a] = [a - 1];
    continue _tailCallLoop;
  }
}

See examples directory for more examples.

About

A babel plugin for performing tail call optimization in recursive functions

Topics

Resources

Stars

Watchers

Forks