简体   繁体   English

CSS和jQuery Selector Speed

[英]CSS and jQuery Selector Speed

In jQuery whenever I encounter something like this: 每当我遇到这样的事情时,在jQuery中:

$("div#MyDiv").....

I generally say to the developer: "Don't bother putting the div in front of #MyDiv , ID selectors are the fastest." 我通常会对开发人员说:“不要把div放在#MyDiv前面,ID选择器是最快的。” Ie

$("#MyDiv")....

This is because the latter will hook directly into document.getElementById rather than having to scan the DOM for all <div> elements first. 这是因为后者将直接挂接到document.getElementById而不是首先扫描所有<div>元素的DOM。

My question is, do the same rules apply to CSS selectors? 我的问题是,相同的规则是否适用于CSS选择器? Ie rather than: 即,而不是:

div#MyDiv
{
}

Is it faster to have simply?: 简单地说它更快吗?:

#MyDiv
{
}

(I realise that CSS selectors are incredibly fast anyway, so in reality neither would make a significant difference.) (我意识到CSS选择器无论如何都非常快,所以实际上两者都不会产生显着差异。)

Many Thanks 非常感谢

EDIT 编辑

Any links, or references might be useful for the purposes of this discussion. 任何链接或引用可能对本讨论的目的有用。 Thanks :-) 谢谢 :-)

I'd say that it's extremely unlikely that it makes any real-world difference. 我会说它不太可能产生任何现实世界的差异。 In theory, yes, there's one fewer check required (because div#foo really does need to be a div to match the selector, according to the spec ). 从理论上讲,是的,只需要少一点检查(因为根据规范div#foo确实需要成为匹配选择器的div )。 But the odds of it making any real difference in a real-world browser app? 但它在现实世界的浏览器应用程序中产生任何真正差异的可能性? Near zero. 接近于零。

That said, I always cringe when I see things like div#foo in HTML applications. 也就是说,当我在HTML应用程序中看到div#foo类的东西时,我总是畏缩不前。 HTML has only one ID-type attribute ( id ), so there's no need for the further qualification. HTML只有一个ID类型属性( id ),因此不需要进一步的限定。 You make the CSS selector engine (either the browser's or jQuery's) work harder to figure out what you mean, you make the selector fragile (if the div becomes a footer , for instance), etc., and of course you do leave yourself open to a stoopid selector implementation that fails to recognize that it can look something up by ID and then check to see if it's a div and so goes looking through all the div s. 你让CSS选择器引擎(或者是浏览器的或jQuery的)更加努力弄清楚你的意思,你做出选择脆弱的(如果div成为一个footer ,例如)等,当然会把自己暴露一个stoopid选择器实现,无法识别它可以通过ID查找, 然后检查它是否是div ,所以查看所有div (Does such an implementation exist? Possibly, you never know.) Barring some edge cases, it always makes me think someone doesn't quite know what they're doing. (这样的实现是否存在?可能,你永远不会知道。)除了一些边缘情况,它总是让我觉得有人不太清楚他们在做什么。

So for me, speed isn't the main argument. 所以对我而言,速度不是主要论点。 Pointlessness is. 毫无意义。 ;-) ;-)

yes this is faster because of parsing speed and because browser must not check if element is also a <div> . 是的,因为解析速度更快,因为浏览器不能检查元素是否也是<div> (for a few rules speed difference is not perceivable by the user) (对于一些规则,用户无法察觉速度差异)

According to this Mozilla article , it does make a difference, although a minute one. 根据这篇Mozilla的文章 ,它确实有所作为,尽管只是一分钟。 (Note that while that article discusses styling XUL user interfaces, the Gecko layout engine is used both for rendering Firefox's user interface and the Web pages it loads.) (请注意,而本文讨论的造型XUL用户界面,Gecko排版引擎渲染Firefox的用户界面和它加载网页使用这两种 )。

The ID is meant to apply to a single element in the DOM anyway, so the performance hit incurred by attempting to match the tag name is completely unnecessary, whether significant or not. ID无论如何都要应用于DOM中的单个元素,因此尝试匹配标记名称所产生的性能损失完全没有必要,无论是否重要。 Not to mention it would waste a few bytes in your stylesheet as well. 更不用说它会浪费你的样式表中的几个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM