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.



Source: Tips4all

3 comments:

  1. The ECMAScript 4 spec was originally going to add support for TCO, but it was dropped.

    http://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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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

    ReplyDelete