Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Thursday, April 19, 2012
Are any Javascript engines tail call 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.
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.
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
The ECMAScript 4 spec was originally going to add support for TCO, but it was dropped.
ReplyDeletehttp://lambda-the-ultimate.org/node/3047
As far as I know, no widely-available implementations of JS currently do automatic TCO. This may be of use to you, though:
http://paulbarry.com/articles/2009/08/30/tail-call-optimization
Essentially, using the accumulator pattern accomplish the same effect.
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.
ReplyDeleteIf it's simple self-recursion, it's probably worth the effort to use explicit iteration rather than hoping for tail-call elimination.
No joy for the moment, but thankfully proper tail calls are slated for Harmony (ECMAScript version 6)
ReplyDeletehttp://wiki.ecmascript.org/doku.php?id=harmony:proper_tail_calls