Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are any JavaScript engines tail call (TCO) optimized?

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.

like image 615
clofresh Avatar asked Sep 07 '10 16:09

clofresh


People also ask

Does Javascript have tail call optimization?

Yes, ES2015 offers tail call optimization in strict mode.

Does javascript support TCO?

Note TCO is a javascript engine implementation feature, it cannot be implemented via a transpiler if the browser does not support it. There is no additional syntax in the spec required to implement TCO and thus there is concern that TCO may break the web.

Does V8 have tail call optimization?

It's worth noting that V8 fully implemented proper tail calls but ultimately removed them, according to their blog post from 2016. To help outline a solution to the aforementioned problems, V8 created a proposal for an alternative approach called syntactic tail calls (STC).

Does node support tail call optimization?

Tail-call optimization is a part of the ES2015-ES6 specification. Supporting it isn't a NodeJS thing, it's something the V8 engine that NodeJS uses needs to support. Node 7.10 down to 6.5. 0 support this in strict mode only and with the flag “--harmony”.


2 Answers

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

No more tail calls in JavaScript?

As far as I know, no widely-available implementations of JavaScript currently do automatic TCO. This may be of use to you, though:

Tail Call Optimization

Essentially, using the accumulator pattern accomplish the same effect.

like image 167
Tim Sylvester Avatar answered Sep 30 '22 03:09

Tim Sylvester


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

like image 23
Mr Speaker Avatar answered Sep 30 '22 01:09

Mr Speaker