Analysis of the maximum number of recursive calls supported by JavaScript _ javascript skills

Source: Internet
Author: User
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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.