//不要用下面的代码(6次属性查找) var qs = window.location.href.substring(window.location.href.indexOf('?')); //应该用下面的代码(4次,而且更具可读性) var url = window.location.href; var qs = url.substring(url.indexOf('?'));
//credit: Jeff Greenberg for JS implementation of Duff’s Device //假设 values.length > 0 var iterations = Math.ceil(values.length /8); var startAt = values.length %8; var i =0; do{switch(startAt){case0:process(values[i++]);case7:process(values[i++]);case6:process(values[i++]);case5:process(values[i++]);case4:process(values[i++]);case3:process(values[i++]);case2:process(values[i++]);case1:process(values[i++]);}startAt =0; }while(--iterations >0); //以上代码来自:http://www.cnblogs.com/kylindai/archive/2013/12/04/3458476.html
或者另一个更快的Duff方法:
//credit: Speed Up Your Site (New Riders, 2003) var iterations = Math.floor(values.length /8); var leftover = values.length %8; var i =0; if(leftover >0){do{process(values[i++]);}while(--leftover >0); } do{process(values[i++]);process(values[i++]);process(values[i++]);process(values[i++]);process(values[i++]);process(values[i++]);process(values[i++]);process(values[i++]); }while(--iterations >0); //以上代码来自:http://www.cnblogs.com/kylindai/archive/2013/12/04/3458476.html