Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the js recursion limits for Firefox, Chrome, Safari, IE, etc?

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?

like image 687
Maciek Avatar asked May 10 '10 18:05

Maciek


People also ask

Are there any restrictions on using recursion JS?

The maximal recursion depth is limited by JavaScript engine. We can rely on it being 10000, some engines allow more, but 100000 is probably out of limit for the majority of them.

How do I fix too much recursion error?

This causes the function to call itself, again and again, making it infinitely recursive. This issue also appears if the same variable is used in the getter. To avoid this problem, make sure that the property being assigned to inside the setter function is different from the one that initially triggered the setter.

What is the maximum recursion depth in C++?

No, C++ does not have an explicit recursion depth. If the maximum stack size is exceeded (which is 1 MB by default on Windows), your C++ program will overflow your stack and execution will be terminated.

What is recursive function in JS?

Recursion is a process of calling itself. A function that calls itself is called a recursive function. The syntax for recursive function is: function recurse() { // function code recurse(); // function code } recurse(); Here, the recurse() function is a recursive function.


2 Answers

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
like image 170
Adam Avatar answered Oct 02 '22 11:10

Adam


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.

like image 45
jpolitz Avatar answered Oct 02 '22 12:10

jpolitz