使用“:not"之间的性能差异和“.not()"选择器? [英] Performance differences between using ":not" and ".not()" selectors?

查看:22
本文介绍了使用“:not"之间的性能差异和“.not()"选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两行之间是否存在任何速度/效率差异.

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()选择器过滤器.在大多数情况下,这是一个更好的选择.

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.

这篇关于使用“:not"之间的性能差异和“.not()"选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆