Tuesday, June 2, 2009

Javascript for() loop vs jQuery .each() performance comparison

This post is an outcome of 15 minutes of free time and a question that I had yesterday. This question were:

  1. How fast jQuery’s .each() method is?
  2. How does it compare to javascript’s native for loop?

It is clear without any performance tests that native javascript for loop is faster, but I always used jQuery’s .each() utility with caution. It always felt like I will get a performance penalty for using it. So I tried to never use it.

So today, I wrote up a little javascript performance test and got my answers. Basically, I created an array and iterated through it using native for loop and jQuery’s .each() iterator. All I did was just an iteration and no array amendments or any other logic. I know it is very basic, but that’s exactly what I want to know. How fast they iterate!

Performance test code:

console.time('TestNative');
length = myArray.length;
for( i=0; i < length; i++){
  myArray[i];
}
console.timeEnd('TestNative');

console.time('TestjQuery');
jQuery.each(myArray, function(i, val) {
  val;
});
console.timeEnd('TestjQuery');

Performance test results:

JavaScript Native FOR loop

Array size    Time
==========    ======
10,000        7ms
100,000       62ms
1,000,000     613ms


jQuery .each() loop

Array size    Time
==========    ======
10,000        10ms
100,000       177ms
1,000,000     1100ms

As you can see native for loop is never over 1ms. That’s probably because we are not doing anything with our array and browser simply optimizes the code or maybe its that fast :)

Usually we don’t have more than 1000 items in our arrays and objects, that is why I guess it can be concluded that using .each() loop in our code will not cause any performance penalties.

14 comments:

  1. By using "lenght" instead of "length", you've optimized the native for loop to run at O(1) speed :)

    ReplyDelete
  2. My results are:
    TestNative: 512ms
    TestjQuery: 2527ms

    ReplyDelete
  3. myArray.lenght => myArray.length

    ReplyDelete
  4. @paxvel, @js Thanks for pointing out.

    I knew there was something wrong here :)

    Updating the post!!!

    ReplyDelete
  5. for( var i=0, l = myArray.length; i < l; i++){
    myArray[i];
    }

    var i = myArray.length;
    while ( i-- ) {
    myArray[i];
    }

    both of these should speed up your native javascript for-loop a bit.

    ReplyDelete
  6. @js, Yeap, saving .length to variable speeds up the process.

    100,000 = 62ms => 38ms
    1000000 = 613ms => 370ms

    ReplyDelete
  7. I've never had an qualms about using $.each ... your post is further confirmation that the performance of $.each vs native JS is negligible.

    ReplyDelete
  8. I've found just the opposite. I'm working on a web app right now that iterates through a huge (300+k, 13,000+ line) JSON array, and the original coder had built it using multiple instances of $.each(). While the app ran fine in Firefox and Safari, IE 7 & 6 would both hang for 30 seconds or more, and then eventually throw their "A script on this page is casing Internet Explorer to run slowly and may cause your computer to become unresponsive" error message.

    I went through his code and replaced all of the $.each() instances with standard for(x in y) loops and now the app loads in about 10 seconds in IE and nearly instantly in Firefox/Safari.

    $.each() is simpler to use and faster to write, and in small loops is still fine to use, but in large applications I'll never use it. It just doesn't scale well.

    ReplyDelete
  9. As a follow up to my last comment, using console.time on $.each() vs for(x in y):

    $.each(): 1668ms
    for(x in y): 85ms


    Um... yeah. Almost TWENTY TIMES FASTER.

    ReplyDelete
  10. Awesome! thanks for showing this testing and result. :)

    ReplyDelete
  11. hi,

    pls give iteration to compare when strings found in jquery

    ex:
    var beatles = Array("john", "paul", "george", "ringo");
    {
    if(found(beatles)
    {
    give alert
    }

    ReplyDelete
  12. Try your test with last jQuery and update your post. "each" is almost 2 times faster than "for".

    ReplyDelete
  13. I tested this in the firebug console with jquery 1.7.1 and jquery each was better:

    var myArray = [0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3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,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,];

    console.time('TestNative');
    length = myArray.length;
    for( i=0; i < length; i++){
    myArray[i];
    }
    console.timeEnd('TestNative');

    console.time('TestjQuery');
    jQuery.each(myArray, function(i, val) {
    val;
    });
    console.timeEnd('TestjQuery');

    ReplyDelete
  14. I always get slightly nervous when someone says "Well, the perfoamcne penalty is only small so lets use xxx". I always say all little things add up. Sure we are not micro-managing performance unless we have to. But making something good practice and a habit which, as a bonus, also boosts performance (not matter how small) is only a god thing. How much trouble is it for someone to simply get into a habbit of not using $.each at all unless they have to for technical reasons? This is the same as caching selectors you use more than once in a function. All those little good habbits add up and do no incease development time but only add positive bonsuses. One should not be nglectfull by nature until they are forced to care but instead be mindfull of their habbits and simply write good code.

    ReplyDelete