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

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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)

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

What is going on here?

回答1:

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.



回答2:

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

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.



回答3:

Different approach to your "task" http://jsfiddle.net/ADMnj/17/

But i guess the performance issues are coming from that you dont state selector the in the right matter

#t tr input:checkbox:checked   VS  #t tr :checkbox:checked  

Tough the right way to check if a checkbox is checked would be to call it like this

#t tr input[checked="checked"] 

W3.ORG's selector list see the E[foo]

And last but not least the fastest is also the one with the shortest code which might be the slight performance different you get from 3 lines vs 4 and 5 lines of code but cant prove this fact



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!