Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of jQuery selectors vs local variables

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?

like image 702
KodeKreachor Avatar asked Apr 07 '12 14:04

KodeKreachor


People also ask

Which jQuery selector is fastest?

ID and Element selector are the fastest selectors in jQuery.

Are jQuery selectors the same as css selectors?

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{...}

Can jQuery selector be a variable?

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.

How do I improve the performance of a jQuery selector?

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 ().

Should I cache my jQuery selectors?

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.

What is the use of pseudo selector in jQuery?

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.

How do I begin a jQuery selector with an ID?

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 ().


2 Answers

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.

like image 120
weotch Avatar answered Sep 30 '22 15:09

weotch


Don't forget this one:

var execute = function(){ 
    $('.myElement')
        .css('color','green')
        .attr('title','My Element')
        .click(function(){ 
            console.log('clicked'); 
        });
}
like image 33
ZippyV Avatar answered Sep 30 '22 15:09

ZippyV