爲什麼我的瀏覽器速度變慢時,這list
數據是在下面的代碼龐大:瞭解遞歸在JavaScript和替代
var list = [];
/*
Creating some huge dummy data
Interstingly, when I change the value of i from 10000
to 100000, the browser hangs(becomes unresponsive)
*/
for(i=0;i<10000;i++){
list.push(i);
}
/*Recursive function*/
var nextListItem = function() {
var item = list.pop();
if (item) {
console.log(item);
// process the list item...
nextListItem();
}
};
nextListItem(); // Commented this as browser becomes unresponsive.
我找不到一個直接的答案,從谷歌,我的問題,所以儘管得到了SO專家的幫助。我認爲它與瀏覽器內存有關,因爲我可以看到循環以很快的速度開始,並且緩慢下降並且變得沒有響應。但不知道爲什麼?
遞歸函數是有限的。我多年來一直嘗試使用它們,但由於這個限制,它們在js中被折磨。限制究竟是什麼? http://stackoverflow.com/questions/2805172/what-are-the-js-recursion-limits-for-firefox-chrome-safari-ie-etc – rottenoats 2014-10-22 07:44:38
@Grimbode謝謝。這看起來不錯...讓我通讀... – Shubh 2014-10-22 07:45:53
@Grimbode:這不是問題在這裏。 – Cerbrus 2014-10-22 07:46:49