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.
Yes, ES2015 offers tail call optimization in strict mode.
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.
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).
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”.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With