Is it recommended that, when I need to access the result of a jQuery selector more than once in the scope of a function, that I run the selector once and assign it to a local variable?
Forgive my trite example here, but i think it illustrates the question. So, will this code perform faster:
var execute = function(){
var element = $('.myElement');
element.css('color','green');
element.attr('title','My Element');
element.click(function(){
console.log('clicked');
});
}
than this code:
var execute = function(){
$('.myElement').css('color','green');
$('.myElement').attr('title','My Element');
$('.myElement').click(function(){
console.log('clicked');
});
}
If there is no difference, can anyone explain why? Does jQuery cache elements after selecting them so subsequent selectors don't have to bother searching the dom again?
ID and Element selector are the fastest selectors in jQuery.
Yes, they are practically equal, jQuery could have some extra selectors documented in http://api.jquery.com/category/selectors but most important: you can use for e.g. $('#foo img') or $('. a, . b, #c') and in your css you can use :checked{...}
Projects In JavaScript & JQueryYes, it is possible to pass a variable into a jQuery attribute-contains selector. The [attribute*=value] selector is used to select each element with a specific attribute and a value containing a string.
A "flatter" DOM also helps improve selector performance, as the selector engine has fewer layers to traverse when looking for an element. Beginning your selector with an ID is a safe bet. With the first approach, jQuery queries the DOM using document.querySelectorAll ().
You can also cache your jQuery selectors for better performance using the ID as your selector (as mentioned in the previous tip). When you don’t cache the selector, jQuery must rescan the DOM to get the element. You may not feel the difference in small applications, but with large applications this becomes very critical.
Pseudo selectors are useful for selecting elements with different states, or a particular element from a set of elements, but they are slower compared to other jQuery selectors. You should either find a way to replace the pseudo selector or be very specific in using it.
Beginning your selector with an ID is a safe bet. With the first approach, jQuery queries the DOM using document.querySelectorAll (). With the second, jQuery uses document.getElementById (), which is faster, although the speed improvement may be diminished by the subsequent call to .find ().
Reusing the selector reference, your first case, is definitely faster. Here's a test I made as proof:
http://jsperf.com/caching-jquery-selectors
The latter case, redefining your selectors, is reported as ~35% slower.
Don't forget this one:
var execute = function(){
$('.myElement')
.css('color','green')
.attr('title','My Element')
.click(function(){
console.log('clicked');
});
}
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