94

I have a tail recursive pathfinding algorithm that I've implemented in JavaScript and would like to know if any (all?) browsers would possibly get stack overflow exceptions.

4
  • 2
    Is it actually a recursive algorithm, or an iterative algorithm implemented with recursion? My understanding is that TCO can only help with the latter. Commented Sep 7, 2010 at 16:32
  • 1
    I just want to add that TCO is not only an optimization. Supporting it should be part of the language specification, not the compiler/interpreter since code written against one interpreter/compiler with TCO would probably not work on an interpreter/compiler without TCO. Commented May 4, 2014 at 18:34
  • 1
    You can see current support and watch it evolve across engines in Kangax's ES6 compatibility table here: kangax.github.io/compat-table/es6/… Commented Dec 16, 2014 at 20:22
  • nmichaels: TCO (Tail Call Optimization) is about any tail call, not just recursive tail calls. Commented Apr 6, 2021 at 16:40

6 Answers 6

47

The ECMAScript 4 specification was originally going to add support for TCO, but it was dropped:

No more tail calls in JavaScript?

As of 2010, no widely-available implementations of JavaScript currently do automatic TCO.


See comments for more recent updates.

Sign up to request clarification or add additional context in comments.

7 Comments

Just an FYI, Rhino has automatic TCO along with Continuations in "interpreted" mode (opt = -1) wiki.apache.org/cocoon/RhinoWithContinuations
(sorry for trolling) ECMAScript 6 has included TCO, termed Proper Tail Calls in the specification.
@sclv: What's the trampoline reference?
The accumulator pattern doesn't accomplish the same effect as TCO. It merely transforms recursive algorithms into tail-recursive form. This is a prerequisite for TCO to be possible, but it is not a substitute for it. You'll still blow the stack in a language that doesn't optimise tail-calls.
"no widely-available implementations of JS currently do automatic TCO" this is incorrect as of Node 6.2.0, if you pass the right flag
|
26

No joy for the moment, but thankfully proper tail calls are slated for Harmony (ECMAScript version 6) http://wiki.ecmascript.org/doku.php?id=harmony:proper_tail_calls

6 Comments

@MarkWilbur The question was specifically about browsers, not all existing implementations of ECMAScript.
@UselessCode Nope, this question is about "Javascript engines" so... not just browsers
@BT There are indeed many non-browser JS environments, and the title does use the more generic "Javascript engines" but the body of the question specifies "...would like to know if any (all?) browsers would possibly get stack overflow exceptions."
I have to counter "but the title says...". I think because he mentions both, the question is about both. But you're right if you're saying it doesn't make the answer obsolete.
@MarkWilbur As far as I am aware node using the same version of v8 as chrome - which currently doesn't support do TCO I had a gist with the JS, and the optimised assembler that current V8 produces - gist.github.com/mcfedr/832e3553964a014621d5
|
13

Tail call optimization will be supported In ECMAScript 6 strict mode in the future. Check http://www.2ality.com/2015/06/tail-call-optimization.html for details.

Check http://kangax.github.io/compat-table/es6/ for current engine support.

At the moment (18-07-2019) the following engines support tail call optimization:

  • Safari >= 10
  • iOS >= 10
  • Kinoma XS6
  • Duktape 2.3

support if "experimental JavaScript features"-flag is turned on:

  • Node 6.5
  • Chrome 54 / Opera 41 Current version of the compat table does not list it anymore

Comments

12

Pretty much every browser you encounter will barf on "too much recursion". Here's an entry in the V8 bug tracker that will probably be interesting reading.

If it's simple self-recursion, it's probably worth the effort to use explicit iteration rather than hoping for tail-call elimination.

2 Comments

The bug has finally been accepted. It is under the epic: "Feature Request Harmony". Hopefully that means that they plan to add it to ES6 support in V8.
You can vote for TCO support in Internet Explorer here: wpdev.uservoice.com/forums/257854-internet-explorer-platform/…
3

Tail call optimization is now available in LispyScript which compiles to JavaScript. You can read more about it here.

1 Comment

What about mutual recursion?
2

Currently no JavaScript implementations recognise tail recursion. Changes are being made in ECMAScript 6, and as others have said, there is an open ticket on V8.

Here you can see V8's generated assembler for a tail recursion function:

Example of how V8 compiles recursion

Compare that to how Clang has compiled the same function in C

Example of C compiler tail recursion

V8 retains the recursive call, whereas the C compiler has recognised the tail recursion and changed it into a loop.

1 Comment

"Currently no JS implementations recognise tail recursion." that is incorrect as of Node 6.2.0, but you gotta pass a flag

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.