Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$().each vs $.each vs for loop in jQuery?

I Can't understand why it is happening.

I read here that :

The first $.each constitutes a single function call to start the iterator.

The second $(foo.vals).each makes three function calls to start the iterator.

  • The first is to the $() which produces a new jQuery wrapper set (Not sure how many other function calls are made during this process).
  • Then the call to $().each.
  • And finally it makes the internal call to jQuery.each to start the iterator.

In your example, the difference would be negligible to say the least. However, in a nested use scenario, you might find performance becoming an issue.

Finally, Cody Lindley in jQuery Enlightenment does not recommend using $.each for iterations greater than 1000 because of the function calls involved. Use a normal for( var i = 0... loop.

So I tested it with this jsperf :

(task : find Tr's who has checked checkbox inside of them , and color that tr.)

This is the jsbin

But look at jsperf

against all expectations , the opposite is the true. ( chrome and FF and IE)

enter image description here

The one who uses $().each ( which calls three methods is the fastest and etc..

What is going on here?

like image 609
Royi Namir Avatar asked Feb 11 '13 08:02

Royi Namir


People also ask

Which loop is faster in jQuery?

each() loop : Lower performance compare to other loops (for games/animations/large datasets) Less control over iterator (skip items, splice items from list, etc). Dependency on jQuery library unlike for, while etc loops!

Which is faster each or for loop?

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

What is the use of jQuery each () function?

each(), which is used to iterate, exclusively, over a jQuery object. 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.


2 Answers

Your test is too heavy to really determine the actual difference between the three looping options.

If you want to test looping, then you need to do your best to remove as much non-related work from the test as possible.

As it stands, your test includes:

  • DOM selection
  • DOM traversal
  • element mutation

All of those are quite expensive operations compared to the loops themselves. When removing the extra stuff, the difference between the loops is much more visible.

http://jsperf.com/asdasda223/4

In both Firefox and Chrome, the for loop is well over 100x faster than the others.

enter image description here

like image 128
the system Avatar answered Sep 20 '22 15:09

the system


Well

  • $.each() is a jQuery function being executed which will be used to iterate over your list, so the overhead should be the jQuery function as well as the overhead of calling for that function for every item in the list. In this case
  • $(thing).each() The idea behind this is that the $(thing) makes an jQuery instance and then you iterate over this instance (.each acts on that instance). In your case, because the instance you called it with is already a jQuery object, the overhead is minimal (are you an instance, oh yes you are).
  • for() In this case there is no overhead at all, except looking up the length of the list on each iteration.

Consider doing:

var l = g.length; for (var i=0;i<l;i++) {     // code; } 

Depending on your HTML most of the time could very well be in the Sizzle Jquery parser finding your selector in the document.

Also note, I don't think your selector is the best, unless things have changed significantly recently jQuery selectors are evaluated right to left, consider limiting the scope of the selector by doing a .find() on everything beneath the first tag referenced by id as it will then be searching only a subset of the document.

like image 42
Forbesmyester Avatar answered Sep 18 '22 15:09

Forbesmyester