Jan
31
2010

Testing Browsers Javascript Engine

Problog

This post cover the following topics:

  • Song of the post
  • Code for testing Javascript engine
  • Results

Song of the Post – James Blackshaw – Cross

I decided to combine my two favorite things – javascript and music. So I’ll try to add a song which either i listened to during the writing of the post, or i just kept listening to it over and over so i decided to let you enjoy it as well (or not, your choice).

It is recommended to read the post while listening to the song (some say it is even better just to listen to the song, i’m just saying…)

So this post is hosting James Blackshaw, the gifted guitarist and pianist from London:

Couple of days ago a friend of mine came to watch a football (or as known to the Americans as soccer) game. The game was so boring that we started to talk about man stuff – who has the biggest, or in our case who is the fastest. After the game was over (thank god) i wanted to know which of the browsers has the most fastest javascript engine. i decided to run a little test of my own.

The Test Code

In order to test the javascript engine of each browser i created a page with an inline javascript code. i decided to check how each browser perform with string manipulation. I wrote two functions that do the same thing:


function strAdd1(){
    var start = new Date().valueOf();
    var str = "This is the string that will be tested. it is a long one with numbers 1234567890";
    for(var i=0;i<100000;i++){
        str += "This is the string that will be tested. it is a long one with numbers 1234567890";
    }
    return new Date().valueOf() - start;
}

function strAdd2(){
    var start = new Date().valueOf();
    var str = new Array();
    for(var i=0;i<100000;i++){
        str[str.length] = "This is the string that will be tested. it is a long one with numbers 1234567890";
    }
    var s = str.join("");
    return new Date().valueOf() - start;
}

function runTest(){
    var results = "Test Results<br>";
    results += "Time for string concotation: " + strAdd1() + "<br>";
    results += "Time for Array Join: " + strAdd2() + "<br>";
    document.getElementById("results").innerHTML = results;
}

addStr1 adds 10,000 times a string using regular string concatenation operator (+).
addStr2 creates an array and adds a new string to the end of the array 10,000 times. at the end it creates the same string as addStr1 does, using the join method. I know this is not the ultimate benchmark for javascript engine (nor even close) but i just wanted to validate my feelings about each browser.

I then ran the test on the following browsers:

  • firefox 3.5.7
  • IE8 8.0
  • safari 4.0.4
  • Chrome 4.0

The results

Browsers Javascript Engine Results

as you can see Chrome left all the others way behind, and to my surpriseĀ  firefox is lagging behind. What’s interesting to me is that i thought that the concatenation string (addStr1) would be much slower then strAdd2, but to my surprise it had much better results. I guess this is due to the fact that browser manufactures have really put an emphasis on javascript performance, and if we would test this code on IE6 and other old browser the picture would be much different.

So my friend won the argument but still, i’ll take firefox over any browser no matter how fast it is.

If you think of another function to add to the test, please write it in the comments to share it with everyone, and maybe i’ll add it to the post.
Cheers – and go Firefox!

Bookmark and Share
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Twitter

tags: , , , , , , ,
posted in Blog by Amir Harel

Follow comments via the RSS Feed | Leave a comment | Trackback URL

  • Leslie_M
    Amir,

    It would be interesting to correlate your tests vs http://www2.webkit.org/perf/sunspider-0.9/sunsp...

    A few other tests would be interesting, if you have the time and interest in doing so. First would be Javascript crypto operations. Check out: http://crypto.stanford.edu/sjcl/

    Another would be DOM access and manipulation. e.g. how fast is each browser at getting a collection of elements and then extracting the values from them. See http://dromaeo.com/
  • Thanks for the info.
    I might use it to write a more comprehensive Javascript engine benchmark.
  • very cool & good post, thank you very much for sharing.
blog comments powered by Disqus
 
Powered by Wordpress and MySQL. Theme by openark.org