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

橙三吉。 提交于 2019-12-17 13:45:23

问题


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

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

and

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

I would think that the first would be better since it is removes objects, but is there an actual difference and is it substantial.

Thanks


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8845811/performance-differences-between-using-not-and-not-selectors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!