循環迭代(2012/11/11) javascript
//原始版本 html for (var i=0; i < items.length; i++){ java process(items[i]); web } spa |
//最小屬性查找,節省25% .net for (var i=0, len=items.length; i < len; i++){ server process(items[i]); htm } blog |
//最小屬性查找並反轉,節省50% ip for (var i=items.length; i--; ){ process(items[i]); } |
//Duff's Device減小迭代次數 var iterations = Math.floor(items.length / 8), startAt = items.length % 8, i = 0; do { switch(startAt){ case 0: process(items[i++]); case 7: process(items[i++]); case 6: process(items[i++]); case 5: process(items[i++]); case 4: process(items[i++]); case 3: process(items[i++]); case 2: process(items[i++]); case 1: process(items[i++]); } startAt = 0; } while (--iterations); |
//Duff's Device取消switch var len = items.length, iterations = Math.floor(len / 8), leftover = len % 8, i = len-1;
while(leftover-- > 0){ process(items[leftover]); };
while(iterations-->0){ process(items[i--]); process(items[i--]); process(items[i--]); process(items[i--]); process(items[i--]); process(items[i--]); process(items[i--]); process(items[i--]); }; |
參考地址:
http://blog.sina.com.cn/s/blog_53a5865c0100if8y.html
http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html