KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
These are mainly just some things I've been wondering, maybe someone can give me a little more insight on them, i'll share what i've noticed so far as well! First thing i've been wondering... is there any difference good or reason to use: $('element').each(function (i, el) { }); -- versus -- $.each($('element'), function (i, el) { }); Looking at the jQuery docs I can't see any rhyme or reason for one or the other (maybe you know an instance or additional things one can do over the other. But more importantly I'm concerned with speed 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 check out this jsFiddle DEMO here , you'll see the difference in speed is basically equivalent with either of them, but more importantly I feel like I should always be using for() loops... I was just unit testing (looping through each of 5 different scenario functions, 50,000 times), simply looping through a bunch of list items, and setting a data-newAttr , nothing special. QUESTION :: I guess my biggest question is, why not always use for loops while iterating through an object?? Is there even a point to using $.each()? Do you always use for() loops even when going 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 sp
Tags (comma-separated)
Save Edits
Cancel