HTML and CSS Reference
In-Depth Information
test();
}
} else {
test(l);
}
var total = new Date().getTime() - start;
var li = document.createElement("li");
li.innerHTML = name + ": " + total +
"ms (total),"+(total / iterations) +
"ms (avg)";
view.appendChild(li);
}, 15);
}(label, tests[label]));
As an example of benchmark 's usage, we can reformat the loop tests using it.
In this example, the length of the array to loop is somewhat reduced, and the total
number of iterations is increased. Listing 4.10 shows the rewritten test. Some of the
tests have been removed for brevity.
Listing 4.10 Using benchmark
var loopLength = 100000;
var array = [];
for (var i = 0; i < loopLength; i++) {
array[i] = "item" + i;
}
benchmark("Loop performance", {
"for-loop": function () {
for(vari=0,item; i < array.length; i++) {
item = array[i];
}
},
"for-loop, cached length": function () {
for(vari=0,l=array.length, item;i<l;i++) {
item = array[i];
}
},
// ...
"double reversed while-loop": function () {
 
Search WWH ::




Custom Search