Official Time Tests
|
|
getElementsByTagName vs evaluategetElementsByTagName is 2,594 ms (9.68 times) faster than evaluate Used 50,000 loops twice over 3.192 seconds total getElementsByTagName time: 299 ms evaluate time: 2,893 ms Function 1 code:
function function1() {
var var1 = document.getElementsByTagName("a");
}
Function 2 code: function function2() {
var var2 = document.evaluate("//a", document, null, 6, null);
}
|
|
|
indexOf vs Regular ExpressionindexOf is 15,539 ms (4.22 times) faster than Regular Expression Used 10,000,000 loops twice over 25.197 seconds total Function 1 time: 4,829 ms Function 2 time: 20,368 ms Function 1 code:
function function1() {
"string".indexOf("tri") != -1;
}
Function 2 code:
function function2() {
/tri/.test("string");
}
|
|
|
forEach vs for()for() is 1,628 ms (12.31 times) faster than forEach. Used 10,000 loops twice over 1.916 seconds total Function 1 time: 1,772 ms Function 2 time: 144 ms Function 1 code:
function function1() {
var links = document.links;
Array.forEach(links, function (e) {});
}
Function 2 code:
function function2() {
var links = document.links;
for (var i = links.length - 1; i >= 0; i--) {}
}Notes: To simply execute, for() is much quicker. Though you don't have to query the array with forEach, so they might weigh about equal depending on what you write. forEach will result in cleaner code with less variable creation though may be a little slower. |
|
|
JoeSimmons wrote: for() is 1,628 ms (12.31 times) faster than forEach. Used 10,000 loops twice over 1.916 seconds total Function 1 time: 1,772 ms Function 2 time: 144 msYour forEach vs for() is not a valid test of the looping speed. How many links are on the document? When am I going to run 10,000 separate loops? It is more important which is the fastest at iterating through a list of elements. Method 1: var a = new Array(1000000);
t1=new Date();
Array.forEach(a, function (e,i) {a[i]=0;});
alert(new Date()-t1);
Time: 121 msMethod 2: var b = new Array(1000000);
t1=new Date();
for (var i = b.length - 1; i >= 0; i--) {b[i]=0}
alert(new Date()-t1);
Time: 993 ms (your method is the slowest)
Method 3: var c = new Array(1000000);
t1=new Date();
for (var i=0, len=c.length; i < len; i++) {c[i]=0}
alert(new Date()-t1);
Time: 88 msMethod 4: var d = new Array(1000000);
t1=new Date();
var i = 0, len = d.length;
while (i<len) {d[i++]=0;}
alert(new Date()-t1);
Time: 83 msMethod 5: var e = new Array(1000000);
t1=new Date();
var i = e.length;
while (--i) {e[i]=0;}
alert(new Date()-t1);
Time: 830 msI am also willing to bet that given a large bit of text to search RegExp is indeed faster than indexOf. |
|
|
sizzlemctwizzle wrote:Read the note I made. I did it on Google so about ~30 links. It didn't do anything with them, I was just seeing which loop ran the fastest without doing anything to the elements. sizzlemctwizzle wrote:Try it for me. And paste the actual results here from the text area using my script. |
|
|
Counting down, direct indexing |
|
|
Thats because your while loop does not execute any iteration. The assignment a=arr[--i] assigns null to 'a' which is the test value and evaluates as false. |
|
|
anderman wrote: Your right. This code shows how many times the inner loop is evaluated: javascript:var i = 500, b = 0, arr = new Array(500);while(a=arr[--i]) {a = 0;b++;}alert(b);
|
|
|
anderman wrote:You're right. Scratch that. Here's a real one showing how you can optimize accessing node lists. Direct indexing for loop vs Non-Direct indexing for loopFunction 1 is 1,381 ms (1.19 times) faster than Function 2 Used 10,000 loops twice over 15.689 seconds total Function 1 time: 7,154 ms Function 2 time: 8,535 ms Function 1 code:function function1() {
var arr = document.links;
for (var i = 0; (a = arr[i]); i++) {
a.href = "";
}
}
Function 2 code:
function function2() {
var arr = document.links;
for (var i = 0; i < arr.length; i++) {
arr[i].href = "";
}
}
|
|
|
Changing function 2 to use this: for (var i = 0, c=arr.length; i < c; i++) { This increases the performance, but its still slower than function1. |
|
|
Function 2 was to show what normal people do versus what should be done with a for loop. |