简体   繁体   English

jquery和CSS中最快的选择器方法 - ID或不?

[英]Fastest selector method in jquery and CSS - ID or not?

What is fastest in jquery/javascript? jquery / javascript中最快的是什么?

$('#myID .myClass')

or 要么

$('.myClass')

What is best to use in CSS? 什么是最好在CSS中使用?

#myID .myClass{}

or 要么

.myClass{}

I see now that I should have explained better. 我现在看到我应该更好地解释。 Sorry! 抱歉!

Ofceauce ID is a faster selector in both CSS and JavaScript. Ofceauce ID是CSS和JavaScript中更快的选择器。 But some times you need to use class since there are multiple selectors. 但有时你需要使用类,因为有多个选择器。

Say forexample that I have i BIG html document. 例如,我有我的大HTML文档。 In the middle of the page I have: 在页面中间我有:

<div id="myID">

<a class="myClass">link1</a>

<a class="myClass">link1</a>

<a class="myClass">link1</a>

</div>

If I want to target all "myClass". 如果我想要定位所有“myClass”。 Would it then be better to target the ID before targeting the classes? 那么在定位类之前定位ID会更好吗? (then I wouldn't have to do domtravel of the entire HTML document) Eg.: (然后我不必对整个HTML文档进行domtravel)例如:

Would this: 这会是:

$('#myID').find('.myClass') $( '#身份识别码')。找到(” MyClass的)

Be faster than: 要快于:

$('.myClass') $(” MyClass的)

My testing on modern browsers suggests that you should go with either, 我对现代浏览器的测试表明你应该选择其中之一,

$('#id').find('.class') // or
$('.class')

but not , 不是

$('#id .class')

Reason being that all modern browsers implement getElementsByClassName resulting in almost-constant time lookups by class name (assuming a hash implementation). 原因是所有现代浏览器都实现了getElementsByClassName导致按类名进行几乎恒定的时间查找(假设是哈希实现)。 Which browsers are modern is another subjective question. 哪种浏览器是现代的另一个主观问题。

They're roughly the same in most modern browsers since class-names are hashed internally. 它们在大多数现代浏览器中大致相同,因为类名在内部进行哈希处理。 The difference is that older browsers don't have a .getElementsByClassName or equivalent method, so .myClass is parsed internally to jQuery and EVERY element in the dom is walked and checked for the classname (or it uses XPath when possible). 不同之处在于较旧的浏览器没有.getElementsByClassName或等效方法,因此.myClass在内部被解析为jQuery,并且dom中的每个元素.myClass.myClass并检查了类名(或者它在可能的情况下使用XPath)。

Always try to use #myID .myClass when possible as it allows jQuery to jump directly to #myID and traverse from there when necessary. 总是尝试尽可能使用#myID .myClass ,因为它允许jQuery直接跳转到#myID并在必要时从那里遍历。

Let's just think logically about this for a second, and pretend that you didn't know anything about how a browser is built internally or how it accesses the DOM, but you assume that whatever it does is logical. 让我们暂时从逻辑上思考这一点,假装你不知道浏览器是如何在内部构建的,或者它是如何访问DOM的,但你认为无论它做什么都是合乎逻辑的。

Therefore, doesn't it stand to reason that out of two selectors, the narrowest one would find you results faster? 因此,是否有理由认为,在两个选择器中,最窄的选择器会更快找到结果?

You have two selectors, which translate to rough english as 你有两个选择器,翻译成粗糙的英语

  1. Any element of the class myClass that is a child of the element with ID of myID myClass类的任何元素,它是ID为myID的元素的子元素
  2. Any element of the class myClass myClass类的任何元素

As for "What is best to use in CSS", this is completely subjective as it depends if you are intending to target all instances of .myClass or just those that are children of #myID . 至于“什么是最好在CSS中使用”,这是完全主观的,因为它取决于你是否打算定位.myClass 所有实例或只是那些#myID#myID

Good question actually. 好的问题其实。

Say you have parsed DOM of N elements of max depth of D and CSS of S number of rules. 假设您已经解析了D个最大深度的N个元素和S个规则的CSS的DOM。 Then the task of finding styles for all elements has computational complexity of roughly O(N*D*S) . 然后,为所有元素找到样式的任务具有大致为O(N*D*S)计算复杂度。

Obviously not all of CSS selectors has the same computation complexity. 显然,并非所有CSS选择器都具有相同的计算复杂度。

For example li.item selector and li[class ~= "item"] require exactly the same CPU resources as they are equivalents. 例如, li.item选择器和li[class ~= "item"]需要完全相同的CPU资源。 li[class = "item"] can be computed faster as it does not require scan of white spaces. li[class = "item"]可以更快地计算,因为它不需要扫描空格。

#1 selector here: #1选择器在这里:

#myID .myClass{} /* #1 */
.myClass{} /* #2 */

require more CPU resources as you need to do exactly the same amount of work as in case #2 plus you will need to scan parent/child chain (of max D elements) to find the element with "myID". 需要更多的CPU资源,因为您需要完成与情况#2完全相同的工作量,您需要扫描父/子链(最大D元素)以找到具有“myID”的元素。

That is all about pure CSS selectors. 这完全是关于纯CSS选择器。

In jQuery & friends situation can be a bit different. 在jQuery和朋友的情况可能会有点不同。 Theoretically jQuery engine can use document.getElementById() to minimize the lookup set (so reduce the N number) but that will not match CSS behavior. 从理论上讲,jQuery引擎可以使用document.getElementById()来最小化查找集(因此减少N数),但这与CSS行为不匹配。 Here is an example: http://jsfiddle.net/dnsUF/ . 这是一个例子: http//jsfiddle.net/dnsUF/ Here jQuery reports one element with #foo but there two such elements in fact. 这里jQuery用#foo报告一个元素,但实际上有两个这样的元素。

Resume: 恢复:

  • In CSS case #2 is faster 在CSS情况下#2更快
  • In jQuery case #1 can be faster (but technically may not be correct in CSS sense). 在jQuery情况下#1可以更快(但在CSS意义上技术上可能不正确)。

Here is my article about CSS selector complexity: http://www.terrainformatica.com/2008/07/csss-and-computational-complexity-of-selectors/ And this one of how to improve it by using style sets: http://www.terrainformatica.com/2010/09/style-sets-in-h-smile-core/ 这是我关于CSS选择器复杂性的文章: http//www.terrainformatica.com/2008/07/csss-and-computational-complexity-of-selectors/这是如何通过使用样式集来改进它: http: //www.terrainformatica.com/2010/09/style-sets-in-h-smile-core/

ID始终是访问元素的最快方式,因为它们是唯一的。

Yeah, id is one of the fastest method to access element. 是的,id是访问元素的最快方法之一。 Check it out this test http://mootools.net/slickspeed/ . 看看这个测试http://mootools.net/slickspeed/

假设您有许多应用.myClass元素, #myID .myClass绝对是访问该元素的更好方法。

Update - 2015 - Check yourself here 更新 - 2015年 - 在这里检查自己
https://jsperf.com/id-vs-class-vs-tag-selectors/2 https://jsperf.com/id-vs-class-vs-tag-selectors/2

TL;DR; TL; DR;

Using ID $("#foo") is almost 4x faster than CSS $(".bar") on my chrome 41 on linux 64bits 使用ID $(“#foo”)比我在linux 64bits上的chrome 41上的CSS $(“。bar”)快4倍

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

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