Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

javascript - 如何避免JavaScript中出现“超出最大调用堆栈大小”错误?(How to avoid 'Maximum call stack size exceeded' error in JavaScript?)

I tried to run the following code in both Firefox and Chrome, it gets run in Firefox but in Chrome: Maximum call stack size exceeded .

(我试图在Firefox和Chrome中都运行以下代码,但该代码可以在Firefox中运行,但在Chrome中运行: Maximum call stack size exceeded 。)

const list = new Array(60000).join('1.1').split('.');

function removeItemsFromList() {
    var item = list.pop();

    if (item) {
        removeItemsFromList();
    }
};

removeItemsFromList();

Is there any way to prevent it and make it run in all the browsers?

(有什么方法可以防止它并使它在所有浏览器中运行?)

  ask by GeniusGo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What about a non recursive version ?

(非递归版本呢?)

const list = new Array(60000).join('1.1').split('.');

function removeItemsFromList() {
    var item = list.pop();

    while (item) {
        item = list.pop();
    }
};

removeItemsFromList();

Here are some numbers on maximum callstack allowed ( source ):

(以下是允许的最大调用堆栈数( ):)

Node.js: 11034

(Node.js:11034)
Firefox: 50994

(的火狐:50994)
Chrome: 10402

(铬:10402)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...