问题描述
以下两行之间是否存在任何速度/效率差异.
Are there any speed/efficiency differences between the following two lines.
$("table td:not(:first-child)")
和
$("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.
谢谢
推荐答案
从 jsperf 测试,:not
平均快两倍.总体而言,尽管此性能可能只占您整体执行时间的一小部分.
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.
jquery 文档状态:
The jquery docs state:
.not() 方法最终将为您提供更具可读性选择而不是将复杂的选择器或变量推入 :not()选择器过滤器.在大多数情况下,这是一个更好的选择.
因此,您实际上可以决定您获得的几分之一秒是否超过可读性.
So really it's up to you to decide if the fractions of a second you gain outweigh the readability.
这篇关于使用“:not"之间的性能差异和“.not()"选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!