javascript 展开循环—学习笔记
<html>
<head><title>js展开循环</title></head>
<body>
<script>
/*credit: Sppeed Up Your Site (New Riders, 2003)*/
/*针对大数据集展开循环的优化方法*/
function duff(values,callback){
var iterations = Math.floor(values.length /8);
var leftover = values.length % 8;
var i = 0;
if(leftover > 0){
do{
callback(i++);
}while(--leftover > 0);
}
do{
callback(i++);
callback(i++);
callback(i++);
callback(i++);
callback(i++);
callback(i++);
callback(i++);
callback(i++);
}while(--iterations > 0);
}
var s = new Array(100000);
duff(s,function(index){
s[index] = index;
document.write(s[index]);
document.write("\n");
});
/*
for(var i = 0; i < s.length; i ++){
s[i] = i;
document.write(s[i]);
document.write("\n");
}
*/
</script>
</body>
</html>