views:

122

answers:

1

I'm currently working on problems in Project Euler with JavaScript. For the most part I've been using for loops to iterate through the problems but wanted to use recursive functions. However, it seems that all of the JavaScript engines have limits to the amount of recursion they can handle.

I compiled/installed SpiderMonkey to try and run from the shell, but still get 18: InternalError: too much recursion

Is there anyway to increase the recursion limit in SpiderMonkey, or is this just a bad idea in general.

Code example:

function cycle(x)
{
    if (check_divisble(x))
    {
        print(i + ' is divisble by 1 - 20' + '\n');
        return;
    }


    x+=20;
    cycle(x);
}

cycle(50400);

Thanks for your help.

+1  A: 

The maximum recursion level is a hard-coded value in the C source.

If you get the source (as described here: https://developer.mozilla.org/En/SpiderMonkey/Build_Documentation), you can change it and compile a new interpreter with your higher value.

Open up js/src/jsinterp.c and find the line containing

#define MAX_INLINE_CALL_COUNT 3000

and change the value at the end to whatever value you want. Keep an eye on your memory usage, since too high a value might kill your machine (or at least make it really laggy).

Also, you probably want to compile the optimized version (as mentioned on the page above), because when memory is freed in the debug version, it overwrites all of it with a set value to make debugging easier, but it can extremely slow down your program (see http://groups.google.com/group/mozilla.dev.tech.js-engine/msg/57934d626c75f7d3).

pib
Highly informative, thanks!
Darren Newton