42

I've got some Javascript code which uses fairly deep recursion and I'd like to find out what the recursion limits in the various browsers are (i.e. the point at which the error "too much recursion" will happen).

Anyone have any solid numbers on this, by version?

Maciek
  • 3,322
  • 6
  • 28
  • 35

2 Answers2

52

Nicholas C. Zakas writes in his blog:

  • Internet Explorer 7: 1,789
  • Firefox 3: 3,000
  • Chrome 1: 21,837
  • Opera 9.62: 10,000
  • Safari 3.2: 500

There's some more data on different browsers and OSs here.

I've created a Browserscope test to get more data. Please run it here.

Update:

The results above are now obsolete, but the browserscope results are updated :

  • IE 11: 12,064
  • Firefox 65: 20,614
  • Chrome 72: 9,643
  • Opera 57: 9,638
  • Safari 12: 32,035
Benoît P
  • 3,179
  • 13
  • 31
Adam
  • 43,763
  • 16
  • 104
  • 144
  • 2
    I threw this together after a VERY fast read of the article: http://jsfiddle.net/TdWTs/ – endemic Apr 10 '13 at 19:41
  • 1
    @endemic Nice work. I made a browserscope for the issue at http://adamrich.name/recursion.html If we can get a lot of people to run the test we could get an up to date table of recusrion limits by browser and OS. – Adam Apr 11 '13 at 17:36
  • My reading comprehension skills need more work... totally skipped over the part where you had already made the Browserscope test. I ran it w/ a few browsers, surprisingly Chrome has the lowest limit! Oh well, a co-worker was worried about a few hundred recursive iterations (wat), so thus my interest in finding the truth. – endemic Apr 11 '13 at 19:19
  • @endemic I just made it now and edited the old answer to add it in. I used your code for the test. Thanks for running it! – Adam Apr 11 '13 at 20:19
  • Are this still applicable as of 2019 – kamentk Feb 05 '19 at 14:21
  • @kamentk no, but you can see some more recent results here: http://www.browserscope.org/browse?category=usertest_ahBzfnVhLXByb2ZpbGVyLWhycg0LEgRUZXN0GL2psRQM – Adam Feb 07 '19 at 17:27
  • Google "Trampoline Recursion" if you need deeper recursion. – Martin Capodici Oct 31 '19 at 01:33
  • It probably relies on how much memory is available to the system somewhat, so results will vary depending on the machine, however I haven't confirmed this. – Fighter178 Mar 30 '23 at 22:56
16

To add to the answers here, this can depend on the functions involved in the recursion, as well. For example, just adding a few parameters to the function can change the result:

var i=0;
function inc() {
    i++;
    inc();
}
inc();

gives me 20923, but

var i=0;
function inc(j, k, l) {
    i++;
    inc(l, k, j);
}
inc(1, 2, 3);

reports 13949 (tested in the console in Chromium 39). Firefox 34 gives 25085 and 13572, respectively.

Adding a try/catch block around the body of the zero-argument inc() gives 11413 frames in Chromium and 13161 in Firefox. With both 3 arguments and the try/catch block, 8967 in Chrome and 7517 in Firefox.

My takeaway from this is that an application that works near the stack depth in a browser can probably only figure this out based on empirical measurements of functions resembling those used in the app.

jpolitz
  • 629
  • 1
  • 8
  • 8
  • The size of a stack frame (local variables are probably allocated on the stack) governs how many stack frames can be allocated (assuming a fixed allocation space, which depends on the OS and/or browser--it is possible to allocate deep stacks almost forever, given an expandable allocation algorithm). You can always simulate the stack using expandable storage such as objects or arrays for the really deep recursion sometimes needed for number theory. – David Spector Nov 26 '18 at 01:23
  • 1
    @DavidSpector indeed, there's even automated solutions for this, like https://www.stopify.org/! – jpolitz May 29 '19 at 00:28