Performance of jQuery Selectors with ID

后端 未结 9 2015
有刺的猬
有刺的猬 2021-01-12 10:52

I know in jQuery if we use ID to select elements,it\'s so efficient.I have a question about this selectors:

please consider this 3 selectors:

         


        
相关标签:
9条回答
  • 2021-01-12 11:39

    The first one is the fastest, simply because it only has 1 property to look for. However,

    document.getElementById("MyElement")
    

    is even faster, though. It's native javascript, and unlike jQuery, the browser immediately knows what you want to do, instead of having to run through a load of jQuery code to figure out what it is you're looking for, in the first place.

    You can use jsPerf to run a speed test, to compare the functions: Test Case. Results:

    $('#MyElement')
    Ops/sec: 967,509
    92% slower
    
    $('#Mytbl #MyElement')
    Ops/sec: 83,837
    99% slower
    
    $('#Mytbl .MyClass')
    Ops/sec: 49,413
    100% slower
    
    document.getElementById("MyElement")
    Ops/sec: 10,857,632
    fastest
    

    Like expected, the native getter is the fastest, followed by the jQuery getter with only 1 selector at less than 10% of the native speed. The jQuery getters with 2 parameters don't even get close to the operations per second of native code, especially the class selector, since classes are usually applied to multiple elements, compared to ID's. (Native ID selectors stop searching after they've found one element, I'm not sure if jQuery does, too.)

    0 讨论(0)
  • 2021-01-12 11:40

    The fastest will be:

      $('#Mytbl', '#MytblContainer' );    
    

    because in this case, jquery don't have to search the whole dom tree to find '#Mytbl'. It will search in the scope given only. IE it will serach in '#MytblContainer' only .

    0 讨论(0)
  • 2021-01-12 11:44

    A couple things:

    • More selectors = slower search. If you can get your desired result with fewer predicates, do so.
    • Getting an element by ID is quicker than getting by a class. getElementById is a core function of JavaScript that is heavily optimised because it is used so frequently. Leverage this when possible.
    • The space selector (' ') is much more costly than the child selector ('>'). If you can use the child selector, do so.

    These rules apply to CSS as they do JavaScript and jQuery.

    0 讨论(0)
  • 2021-01-12 11:45

    this is the way to stop times between some javascript calls

    selectorTimes = [];
    var start = new Date().getTime();
    $('#MyElement')
    selectorTimes.push(new Date().getTime()-start);
    
    start = new Date().getTime()
    $('#Mytbl #MyElement')
    selectorTimes.push(new Date().getTime()-start);
    
    start = new Date().getTime()
    $('#Mytbl .MyClass')
    selectorTimes.push(new Date().getTime()-start);
    
    console.log(selectorTimes);
    

    i think the second selector is not efficient, if you have a domid select this directly: $('#MyElement')

    0 讨论(0)
  • 2021-01-12 11:45

    Here you are. See the comments on top of each example:

    //fastest because it is just one id lookup: document.getElementById("MyElement") with no further processing.
    $('#MyElement')
    //a little slower. JQuery interprets selectors from right to left so it first looks up for #MyElement and then checks if it is hosted in #Mytbl
    $('#Mytbl #MyElement')
    //the slowest of all. JQuery interprets selectors from right to left so it first finds all elements with .MyClass as their class and then searches for those hosted in #Mytbl.
    $('#Mytbl .MyClass')
    

    If you can, always use just the id (like the first example) but if you have to have multiple selectors and classes chained together, try to put the most strict one at the right. For example an id. Because JQuery interprets selectors from right to left.

    0 讨论(0)
  • 2021-01-12 11:46

    I would say that the first one is the fastest because you're simply searching for one id.

    And

    $('#Mytbl .MyClass')
    

    is the slowest because you're not specifying the type of element that has the class "MyClass"

    0 讨论(0)
提交回复
热议问题