javascript 展开循环—学习笔记 博客分类: javascript javascript代码优化
程序员文章站
2024-02-13 10:23:58
...
<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>
<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>
上一篇: Java集合类中文介绍