Measure Function Execution Times

By JoeSimmons Last update May 13, 2009 — Installed 3,099 times.

Official Time Tests

in
Subscribe to Official Time Tests 11 posts, 3 voices



JoeSimmons Script's Author

getElementsByTagName vs evaluate


getElementsByTagName 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);
}

 
JoeSimmons Script's Author

indexOf vs Regular Expression


indexOf 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");
}

 
JoeSimmons Script's Author

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.

 
sizzlemctwizzle Scriptwright
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 ms
Your 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 ms

Method 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 ms

Method 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 ms

Method 5:
var e = new Array(1000000);
t1=new Date();
var i = e.length;
while (--i) {e[i]=0;}
alert(new Date()-t1);
Time: 830 ms
I am also willing to bet that given a large bit of text to search RegExp is indeed faster than indexOf.
 
JoeSimmons Script's Author
sizzlemctwizzle wrote:
Your forEach vs for() is not a valid test of the looping speed. How many links are on the document?
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:
I am also willing to bet that given a large bit of text to search RegExp is indeed faster than indexOf.
Try it for me. And paste the actual results here from the text area using my script.
 
JoeSimmons Script's Author

Counting down, direct indexing while() vs Counting up, non-direct indexing for() loop.

while() is 2,695 ms (63.67 times) faster than for()
Used 50,000 loops twice over 2.781 seconds total
while() time: 43 ms
for() time: 2,738 ms

while() code:

function function1() {
    var i = 500, arr = new Array(500);
    while(a=arr[--i]) {
        a = 0;
    }
}
for() code:
function function2() {
    var arr = new Array(500);
    for (var i=0; i<500; i++) {
        arr[i] = 0;
    }
}

 
anderman User

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.

 
sizzlemctwizzle Scriptwright

anderman wrote:
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.

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);

 
JoeSimmons Script's Author
anderman wrote:
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.
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 loop

Function 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 = "";
    }
}
 
anderman User

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.

 
JoeSimmons Script's Author

Function 2 was to show what normal people do versus what should be done with a for loop.

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel