This article mainly introduces the analysis of the maximum number of recursive calls supported by JavaScript, also known as the maximum number of stacks supported by JavaScript, for more information about how many recursive calls can be made by the JavaScript engine?
Number of recursive calls
The following function can help you find the answer: (inspired by Ben Alman's gist)
The Code is as follows:
Function computeMaxCallStackSize (){
Try {
Return 1 + computeMaxCallStackSize ();
} Catch (e ){
// Call stack overflow
Return 1;
}
}
Three results:
The Code is as follows:
Node. js: 11034
Firefox: 1, 50994
Chrome: 1, 10402
What do these numbers represent? Mr. Aleph pointed out that in V8, the number of recursive calls depends on two quantities: the size of the stack and the size of the stack frame (the local variable that saves the parameter. You can add a local variable in computeMaxCallStackSize () for verification-it returns a low value.
Tail call Optimization in ECMAScript 6
ES6 has tail call optimization: if the last step in a function is also a function call, it will be "skipped" rather than called through a subfunction. This means that in ES6 (strict mode), you only need to slightly change the computeMaxCallStackSize function, and it can be executed forever.
The Code is as follows:
Function computeMaxCallStackSize (size ){
Size = size | 1;
Return computeMaxCallStackSize (size + 1 );
}