Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use jQuery.each()?

I'm doing very frequent iterations over arrays of objects and have been using jQuery.each(). However, I'm having speed and memory issues and one of the most called methods according to my profiler is jQuery.each(). What's the word on the street about its performance? Should I switch to a simple for loop? Of course I'm fixing the many issues in my own code too.

like image 880
pr1001 Avatar asked Dec 10 '09 20:12

pr1001


People also ask

What is the use of jQuery each () function?

The $.each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

Is jQuery each slow?

This article (#3) ran some performance tests and found that the jQuery . each function was about 10x as slow as the native javascript for loop. Using Firebug, it's possible to measure the time each of the two functions takes to run.

Can we use foreach in jQuery?

each() function, jQuery's foreach equivalent. jQuery's foreach equivalent can be very useful for many situations. These examples will get you started and teach you how you can loop through arrays, objects and all kinds of HTML elements.

Which is faster for each or for loop?

The forloop is faster than the foreach loop if the array must only be accessed once per iteration.


2 Answers

This article (#3) ran some performance tests and found that the jQuery .each function was about 10x as slow as the native javascript for loop.

From 10 Ways to Instantly Increase Your jQuery Performance - 3. Use For Instead of Each
Using Firebug, it's possible to measure the time each of the two functions takes to run.

var array = new Array (); for (var i=0; i<10000; i++) {     array[i] = 0; }  console.time('native'); var l = array.length; for (var i=0;i<l; i++) {     array[i] = i; } console.timeEnd('native');  console.time('jquery'); $.each (array, function (i) {     array[i] = i; }); console.timeEnd('jquery'); 

The above results are 2ms for native code, and 26ms for jQuery's "each" method. Provided I tested it on my local machine and they're not actually doing anything (just a mere array filling operation), jQuery's each function takes over 10 times as long as JS native "for" loop. This will certainly increase when dealing with more complicated stuff, like setting CSS attributes or other DOM manipulation operations.

like image 87
Jace Rhea Avatar answered Oct 08 '22 01:10

Jace Rhea


The source code for jQuery's each is as follows (Thank you John Resig and MIT License):

each: function( object, callback, args ) {     var name, i = 0, length = object.length;      if ( args ) {         if ( length === undefined ) {             for ( name in object )                 if ( callback.apply( object[ name ], args ) === false )                     break;         } else             for ( ; i < length; )                 if ( callback.apply( object[ i++ ], args ) === false )                     break;      // A special, fast, case for the most common use of each     } else {         if ( length === undefined ) {             for ( name in object )                 if ( callback.call( object[ name ], name, object[ name ] ) === false )                     break;         } else             for ( var value = object[0];                 i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}     }      return object; } 

As you can trace and see, in most cases it is using a basic for loop where the only overhead is really just the callback itself. Shouldn't make a difference in performance.

EDIT: This is with the realization that selector overhead has already occurred and you are given a populated array object.

like image 28
Justin Swartsel Avatar answered Oct 08 '22 03:10

Justin Swartsel