.find() 比基本后代选择方法更快吗?
Paul Irish 的博客中的幻灯片 30 提到:
$('#container').find( 'div.robotarm')
比 $('#container div.robotarm')
更快
这是真的吗?
Slide 30 in Paul Irish's blog mentioned:
$('#container').find('div.robotarm')
is faster than $('#container div.robotarm')
Is this true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许在 jQuery 的早期版本中就是这种情况。但是,该表达式
通过 jQuery 标准化为
So
$('#container div.robotarm')
应该较慢的唯一原因是函数调用开销。但是,这确实是一个微不足道的差异。如果该调用未标准化,则将使用
sizzle
(Resigs css 选择器引擎)来查找该元素(从右到左)。那当然会慢很多。Maybe in an earlier version of jQuery that was the case. However, the expression
is normalized through jQuery into
So the only reason why
$('#container div.robotarm')
should be slower is because of function call overhead. But, that would really be a trivial difference.If that call wasn't normalized,
sizzle
(Resigs css selector engine) would be used to lookup that element (right to left). That of course would be much slower.既然你征求了意见,那就无所谓了。
您总是可以想到这样一种情况:在某种浏览器中,在某种 DOM 配置下,一个浏览器的运行速度比另一个快。无需吹毛求疵。
Since you asked for opinion, it doesn't matter.
You can always come up with a case where one runs faster than the other in some browser under a certain configuration of the DOM. No need to split hairs.
仅当通过 ID 搜索时这才是正确的。
但是,当我们按标签名称搜索时,它在现代浏览器中返回不同的结果,其中
$('div').find('p')
比$('div p')
慢code> 因为后者使用querySelector()
。This is only correct when searching by ID.
But when we search by tag name it returns different results in modern browsers where
$('div').find('p')
is slower than$('div p')
because the latter usesquerySelector()
.