I am finding that this:
var $colSelect = $('#stuff').find('.things');
$($colSelect).find('img, input, select').addClass("foo");
is slower then this:
var $colSelect = $('#stuff').find('.things');
$($colSelect).find('img').addClass("foo");
$($colSelect).find('input').addClass("foo");
$($colSelect).find('select').addClass("foo");
Can anyone help me understand why that is the case? In specific I am noticing the performance increase in IE8.
My guess would be that for the first selector, jQuery must parse the selector, call JavaScript's getElementsByTagName
for each and join the results (maybe even ordering them in the order of their position in the DOM).
The second case has straight-forward calls to JavaScript's getElementsByTagName
, with no other operations necessary.
This page compares performances: http://jsperf.com/jquery-selector-performance-problems
Update 2: Modified to reflect the grid like nature of what you have. Here's my findings with jQuery 1.7.1 (one is your top code, two is your bottom code). I took only the top 5 slowest things because everything else didn't matter.
As you can see, the reason that one is probably slower than the other is due to the function sortOrder (minified as U) and/or Array.sort. Both of these don't even show up in Two. The issue definately is with having to go back and sort the results. makeArray (minified as s above) seems to take less time in 1, but that doesn't offset the sorting that has to be done.
Update: I created a jsFiddle to play around with it, and I'm not seeing a noticeable difference in IE8 between the two methods. Perhaps more context is needed into what this page looks like / what styles are being applied. It may have something to do with the browser rendering which is causing your code to act differently.
IE8 has a built in profiler.
Tips for making your javascript better. Try not to call $($colSelect) all of the time. Save it to a variable like so.
var $colSelect = $($('#stuff').find('.things'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With