function func2(){ var start = new Date().getTime(); var array = []; for(var i = 0; i < 10000; i++){ array[i] = ""; }
二,for 循环:先把长度算出来直接调用
代码如下:
function func2(){ var divs = document.getElementsByTagName("div"); var start = new Date().getTime(); for(var i = 0, len = divs.length; i < len; i++){ //"效率高" }
三,减少页面的重绘:可以用一中把页面拼接起来然后再赋值给页面
代码如下:
function func2(){ var obj = document.getElementById("demo"); var start = new Date().getTime(); var arr = []; for(var i = 0; i < 100; i++){ arr[i] = str + i; } obj.innerHTML = arr.join("");
四,减少作用域链上的查找次数:如果取多个页面值则定义一个document对象,再调用这个对象
代码如下:
var doc = document; for(var i = 0; i < 10000; i++){ var but1 = doc.getElementById("but1"); var but2 = doc.getElementById("but2"); var inputs = doc.getElementsByTagName("input"); } }
//效率低的 function func1(){ var start = new Date().getTime(); var template = ""; for(var i = 0; i < 10000; i++){ template += ""; } var end = new Date().getTime(); document.getElementById("one").innerHTML = template; alert("用时:" + (end - start) + "毫秒"); } //效率高的 function func2(){ var start = new Date().getTime(); var array = []; for(var i = 0; i < 10000; i++){ array[i] = ""; } var end = new Date().getTime(); document.getElementById("one").innerHTML = array.join(""); alert("用时:" + (end - start) + "毫秒"); }
var arr = []; for(var i = 0; i < 10000; i++){ arr[i] = "
" + i + "
"; } document.body.innerHTML += arr.join("");
//效率低的 function func1(){ var divs = document.getElementsByTagName("div"); var start = new Date().getTime(); for(var i = 0; i < divs.length; i++){ //"效率低" } var end = new Date().getTime(); alert("用时:" + (end - start) + "毫秒"); } //效率高的 function func2(){ var divs = document.getElementsByTagName("div"); var start = new Date().getTime(); for(var i = 0, len = divs.length; i < len; i++){ //"效率高" } var end = new Date().getTime(); alert("用时:" + (end - start) + "毫秒"); }
var arr2 = []; for(var i = 0; i < 10000; i++){ arr2[i] = "
" + i + "
"; } //效率低的 function func1(){ var start = new Date().getTime(); for(var i = 0; i < arr2.length; i++){ //"效率低" } var end = new Date().getTime(); alert("用时:" + (end - start) + "毫秒"); } //效率高的 function func2(){ var start = new Date().getTime(); for(var i = 0, len = arr2.length; i < len; i++){ //"效率高" } var end = new Date().getTime(); alert("用时:" + (end - start) + "毫秒"); }
"; //效率低的 function func1(){ var obj = document.getElementById("demo"); var start = new Date().getTime(); for(var i = 0; i < 100; i++){ obj.innerHTML += str + i; } var end = new Date().getTime(); alert("用时 " + (end - start) + " 毫秒"); } //效率高的 function func2(){ var obj = document.getElementById("demo"); var start = new Date().getTime(); var arr = []; for(var i = 0; i < 100; i++){ arr[i] = str + i; } obj.innerHTML = arr.join(""); var end = new Date().getTime(); alert("用时 " + (end - start) + " 毫秒"); }
function func1(){ var start = new Date().getTime(); for(var i = 0; i < 10000; i++){ var but1 = document.getElementById("but1"); var but2 = document.getElementById("but2"); var inputs = document.getElementsByTagName("input"); var divs = document.getElementsByTagName("div"); var but1 = document.getElementById("but1"); var but2 = document.getElementById("but2"); var inputs = document.getElementsByTagName("input"); var divs = document.getElementsByTagName("div"); var but1 = document.getElementById("but1"); var but2 = document.getElementById("but2"); var inputs = document.getElementsByTagName("input"); var divs = document.getElementsByTagName("div"); var but1 = document.getElementById("but1"); var but2 = document.getElementById("but2"); var inputs = document.getElementsByTagName("input"); var divs = document.getElementsByTagName("div"); var but1 = document.getElementById("but1"); var but2 = document.getElementById("but2"); var inputs = document.getElementsByTagName("input"); var divs = document.getElementsByTagName("div"); var but1 = document.getElementById("but1"); var but2 = document.getElementById("but2"); var inputs = document.getElementsByTagName("input"); var divs = document.getElementsByTagName("div"); } var end = new Date().getTime(); alert("用时 " + (end - start) + " 毫秒"); } function func2(){ var start = new Date().getTime(); var doc = document; for(var i = 0; i < 10000; i++){ var but1 = doc.getElementById("but1"); var but2 = doc.getElementById("but2"); var inputs = doc.getElementsByTagName("input"); var divs = doc.getElementsByTagName("div"); var but1 = doc.getElementById("but1"); var but2 = doc.getElementById("but2"); var inputs = doc.getElementsByTagName("input"); var divs = doc.getElementsByTagName("div"); var but1 = doc.getElementById("but1"); var but2 = doc.getElementById("but2"); var inputs = doc.getElementsByTagName("input"); var divs = doc.getElementsByTagName("div"); var but1 = doc.getElementById("but1"); var but2 = doc.getElementById("but2"); var inputs = doc.getElementsByTagName("input"); var divs = doc.getElementsByTagName("div"); var but1 = doc.getElementById("but1"); var but2 = doc.getElementById("but2"); var inputs = doc.getElementsByTagName("input"); var divs = doc.getElementsByTagName("div"); var but1 = doc.getElementById("but1"); var but2 = doc.getElementById("but2"); var inputs = doc.getElementsByTagName("input"); var divs = doc.getElementsByTagName("div"); } var end = new Date().getTime(); alert("用时 " + (end - start) + " 毫秒"); }
var sum, num1 = 1, num2 = 2; function func1(){ var start = new Date().getTime(); for(var i = 0; i < 10000; i++){ var func = new Function("sum+=num1;num1+=num2;num2++;"); func(); } var end = new Date().getTime(); alert("用时 " + (end - start) + " 毫秒"); }
function func2(){ var start = new Date().getTime(); for(var i = 0; i < 10000; i++){ sum+=num1; num1+=num2; num2++; } var end = new Date().getTime(); alert("用时 " + (end - start) + " 毫秒"); }
var sum, num1 = 1, num2 = 2; function func1(){ var start = new Date().getTime(); for(var i = 0; i < 1000; i++){ eval("sum+=num1;num1+=num2;num2++;"); } var end = new Date().getTime(); alert("用时 " + (end - start) + " 毫秒"); } function func2(){ var start = new Date().getTime(); for(var i = 0; i < 1000; i++){ sum+=num1; num1+=num2; num2++; } var end = new Date().getTime(); alert("用时 " + (end - start) + " 毫秒"); }