Performance differences between using “:not” and “.not()” selectors?

后端 未结 2 1987
生来不讨喜
生来不讨喜 2020-12-03 21:04

Are there any speed/efficiency differences between the following two lines.

$(\"table td:not(:first-child)\")

and

$(\"tab         


        
相关标签:
2条回答
  • 2020-12-03 21:13

    Depends on the browser.

    Browsers that support querySelectorAll will get a performance boost with...

    $("table td:not(:first-child)")
    

    ...because it is a valid selector. Older browsers (IE7 and lower) will not.

    You need to be careful with the :not() selector though. jQuery (Sizzle) extends it with non-standard selectors, so it's easy to break qSA.

    0 讨论(0)
  • 2020-12-03 21:27

    As you can see from the jsperf test, :not is on average about twice as fast. Overall though this performance will likely be a very small part of your overall execution time.

    The jquery docs state:

    The .not() method will end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter. In most cases, it is a better choice.

    So really it's up to you to decide if the fractions of a second you gain outweigh the readability.

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