Wednesday, June 15, 2005

Too much recursion

Press this to see how even the high-level scripting languages in the mainstream prevent programmers from using recursion.

Different Javascript engines will give you different error messages, but the Mozilla one is the most offensive: "too much recursion." Groan.

Update: I've modified the Javascript so it should work in Safari, too. But I'm not going to spend more time making a broken script work in multiple browsers.

3 comments:

Anonymous said...

> Different Javascript engines will
> give you different error messages,
> but the Mozilla one is the most
> offensive: "too much recursion."

In my application, IE 6 had the worst behavior. It just crashed. Using Firefox I got the "Too much recursion" message, which then led me to this page, and then it took me a few seconds to figure out where I had an infinite recursion problem.

Interestingly enough, IE 6 does not crash with your example.

Anonymous said...

FWIW in Firefox 3.5.5 on a win2k box the limit appears to be 3000 calls.

Too bad finding documentation of that is nontrivial.

That said, expecting javascript to support an infinitely large stack is a bit silly -- if you know you are doing deep recursion you should be creating your own custom "stack".

http://www.htmlgoodies.com/primers/jsp/article.php/3622321

is a useful guide.

Dave Herman said...

That said, expecting javascript to support an infinitely large stack is a bit silly -- if you know you are doing deep recursion you should be creating your own custom "stack".

Absolutely not.