These are basically just some of the things that I was interested in, maybe someone can give me a little more information about them, I will share what I have noticed so far!
The first thing I was wondering ... is there any difference or reason to use:
$('element').each(function (i, el) { });
- against -
$.each($('element'), function (i, el) { });
Looking at jQuery docs, I don't see any rhymes or reasons for one or the other (maybe you know an instance or additional things that can be done with each other.
But, more importantly, speed bothers me here
// As opposed to $.each() looping through a jQuery object // -- 8x faster for (var i = 0, $('.whatever').length; i < len; i++) { $('.whatever')[i] // do stuff }
If you look at jsFiddle DEMO here , you will see that the difference in speed is basically equivalent to any of them, but more importantly, I feel I should always use for() loops ...
I was just unit testing (looping through each of 5 different script functions, 50,000 times), just sorting through a bunch of list items and installing data-newAttr , nothing special.
QUESTION:: I think my biggest question is: why is it not always used for loops, iterating through an object? Does it make sense to use $ .each ()? Do you always use for () loops even when passing through jQuery objects?
jsFiddle DEMO here
Function type: Execution Time: _testArea.each() + $(this) 1947 <-- using $(this) slows it down tremendously $.each() + $(this) 1940 _testArea.each() + el(plain JS) 458 <-- using the Element speeds things up $.each() + el(plain JS) 452 for() loop + plainJS[0] iteration 236 <-- over 8x faster
Just my 2cents. :)
javascript jquery
Mark Pieszak - DevHelp.Online Aug 09 '12 at 16:05 2012-08-09 16:05
source share