运行时间 Timed Code
批量处理时,如果每次只执行一个任务,显然效率不高,如果能在不影响用户体验和不会卡住页面的前提下,一次执行多个将助于提升体验,运行时间也减少。
运行时间最长为100毫秒,建议50。
运行时间即一开始,一结束,相差即所花时间
var start = +new Date(); +号帮把Date转为数字
var stop;
doSomeThing();
stop = +new Date();
现在就有了优化数组处理模式的代码
function timedProcessArray(items,process,callback) {
var todo = items.concat();
setTimeout(function(){
var start = +new Date();
do{
process(todo.shift());
} while(todo.length > 0 && (+new Date() - start < 50));
if(todo.length > 0) {
setTimeout(arguments.callee,25);
} else {
callback(items);
}
},25);
}