使用“:not”和“.not()”选择器之间的性能差异?

时间:2021-03-29 20:10:08

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

谢谢

2 个解决方案

#1


25  

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.

正如你在jsperf测试中看到的那样:平均不是快两倍。总体而言,这种性能可能只占整体执行时间的很小一部分。

The jquery docs state:

jquery文档声明:

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.

与将复杂选择器或变量推送到:not()选择器过滤器相比,.not()方法最终会为您提供更多可读选择。在大多数情况下,这是一个更好的选择。

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

所以真的由你决定你获得的一秒的分数是否超过可读性。

#2


13  

Depends on the browser.

取决于浏览器。

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

支持querySelectorAll的浏览器将通过...获得性能提升

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

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

...因为它是一个有效的选择器。较旧的浏览器(IE7及更低版本)不会。

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.

你需要小心:not()选择器。 jQuery(Sizzle)使用非标准选择器扩展它,因此很容易打破qSA。

#1


25  

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.

正如你在jsperf测试中看到的那样:平均不是快两倍。总体而言,这种性能可能只占整体执行时间的很小一部分。

The jquery docs state:

jquery文档声明:

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.

与将复杂选择器或变量推送到:not()选择器过滤器相比,.not()方法最终会为您提供更多可读选择。在大多数情况下,这是一个更好的选择。

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

所以真的由你决定你获得的一秒的分数是否超过可读性。

#2


13  

Depends on the browser.

取决于浏览器。

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

支持querySelectorAll的浏览器将通过...获得性能提升

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

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

...因为它是一个有效的选择器。较旧的浏览器(IE7及更低版本)不会。

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.

你需要小心:not()选择器。 jQuery(Sizzle)使用非标准选择器扩展它,因此很容易打破qSA。