优化执行
// 如果 list 很大,下面的这段递归代码会造成堆栈溢出。如果在不改变递归模式的前提下修善这段代码?
var list = getList();
var nextListItem = function () {
var item = list.pop();
if (item) {
// process the list item...
nextListItem();
}
};
// 原文上的解决方式是加个定时器:
var list = getList();
var nextListItem = function () {
var item = list.pop();
if (item) {
// process the list item...
setTimeout(nextListItem, 0);
}
};