Home > Web Front-end > JS Tutorial > body text

Analysis of the maximum number of recursive calls supported by JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:43:11
Original
1899 people have browsed it

Are you curious about how many recursive calls the JavaScript engine can make?

How many recursive calls

The following function will let you find the answer: (Inspired by Ben Alman's gist)

Copy code The code is as follows:

function computeMaxCallStackSize() {
Try {
            return 1 computeMaxCallStackSize();
} catch (e) {
// Call stack overflow
Return 1;
}
}

Three results:

Copy code The code is as follows:

Node.js: 11034
Firefox: 50994
Chrome: 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 holds the parameters). You can verify this by adding a local variable in computeMaxCallStackSize() - it will return the 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" instead of being called through a sub-function. This means that under ES6 (strict mode), you only need to change the computeMaxCallStackSize function slightly and it can execute forever.

Copy code The code is as follows:

function computeMaxCallStackSize(size) {
size = size || 1;
Return computeMaxCallStackSize(size 1);
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!