简体   繁体   English

jQuery:选择器性能

[英]JQuery: selector performance

I have the following HTML: 我有以下HTML:

...
<div id="panel">
    <div class="abc">
        <p class="xyz">Hello</p>
    </div>
</div>
...

Question: What's the fastest way with JQuery for me to access the p.xyz element? 问题:使用JQuery对我来说访问p.xyz元素的最快方法是什么? I've read some performance reviews but they don't account for all different scenarios. 我已经阅读了一些性能评测,但是它们并不能说明所有不同的情况。

I could do the following, but don't know how to test which is the fastest. 我可以执行以下操作,但是不知道如何测试最快的。

$('.xyz');
$('p.xyz');
$('#panel .xyz');
$('#panel p.xyz');
$(".xyz", $('#panel'));

I'm sure there are even more ways to access that node as well. 我确信还有更多访问该节点的方法。

>>> Which is the fastest and recommended way? >>> 哪种是最快的推荐方法?

Any time you can use an ID selector it will be faster. 只要您可以使用ID选择器,它就会更快。 jQuery selector engine (Sizzle) reads from right to left. jQuery选择器引擎(Sizzle)从右到左读取。 The only exception being if on the far left is an id , then that id is used to scope the search. 唯一的例外是,如果最左边是id ,则该id用于确定搜索范围。

Additionally, since jQuery uses native browser functionality wherever possible, always supply the tag name when using a class selector. 此外,由于jQuery尽可能使用本机浏览器功能,因此在使用类选择器时始终提供标签名称。

In your series of options as to "what is faster", this would perform the fastest: 在有关“更快”的一系列选项中,这将以最快的速度执行:

$('#panel p.xyz');

Because it would scope the search to a single element, then find the p tags, then drop out of the selection all the ones that don't have the proper class. 因为它将搜索范围限定为单个元素,然后找到p标记,然后从选择中删除所有没有适当类的标记。

In some browsers, with a lot of other sibling p tags that shouldn't match your query, it will perform a lot faster than: 在某些浏览器中,具有许多其他符合您查询条件的同级p标签,它的执行速度将比:

$('#panel .xyz');

Just remember that jQuery uses as many native features as possible. 请记住,jQuery使用了尽可能多的本机功能。 Every single mainstream browser has getElementById so use an id where possible. 每个主流浏览器都有getElementById因此请尽可能使用id

Next, every browser has a getElementsByTagName . 接下来,每个浏览器都有一个getElementsByTagName What some browsers do not have is getElementsByClassName . 一些浏览器没有的是getElementsByClassName So help jQuery help you by using an id where possible to scope the search, and by pairing tag names with the class for class searches. 因此,通过在可能的范围内使用id以及将标记名称与类配对以进行类搜索,jQuery可以帮助您。

Never pass a jQuery object as the scope parameter 永远不要将jQuery对象作为作用域参数传递

One thing you should NEVER do, is your last example: 最后一个示例是您永远不应该做的一件事:

$(".xyz", $('#panel'));

That would have no speed impact (and in a loop it would be slower) than using a normal string. 与使用普通字符串相比,这不会对速度产生影响(并且在循环中它会更慢)。 Only every pass a DOM element into this parameter. 只有每个将DOM元素传递到此参数。 This can be very useful when you have the DOM element already: 当您已经拥有DOM元素时,这将非常有用:

$("#panel").click(function(){
  var p = $("p.xyz", this); // This is fast
});

The fastest selector is always the ID selector, or a selector that descends from it. 最快的选择器始终是ID选择器,或者是从其选择的选择器。 In your case: 在您的情况下:

$('#panel .xyz'); and $('#panel p.xyz');

will have about the same speed, so close it doesn't matter...but the non-ID ones are orders of magnitude behind in performance. 速度几乎相同,所以关闭并不重要...但是非ID的性能落后几个数量级。

There's also already a healthy dose of answers on this in another question on SO . 在SO的另一个问题中,对此也已经有健康的答案。

"...but don't know how to test which is the fastest." “ ...但是不知道如何测试最快的。”

Have a look at firebug. 看看萤火虫。 In particular, the Javascript profiler. 特别是Javascript Profiler。 Basically: 基本上:

  • Install the firebug Firefox plugin. 安装firebug Firefox插件。
  • Right click on the little bug icon at the bottom right of firefox. 右键单击Firefox右下角的小错误图标。 Select 'enable all panels'. 选择“启用所有面板”。
  • Click on the 'console' tab. 单击“控制台”选项卡。
  • Click on the 'profile' option. 点击“个人资料”选项。 Hovering the mouse over it says 'Profile Javascript execution time'. 将鼠标悬停在其上方时会显示“配置文件Javascript执行时间”。 The rest should be fairly intuitive. 其余的应该相当直观。 If you find that it is not, there are plenty of tutorials to help you out. 如果您发现事实并非如此,则有很多教程可以帮助您。

You can now experiment with different selectors/DOM structures and measure their relative execution times for a better understanding of which selectors actually perceivably impact the performance of your web application. 现在,您可以尝试使用不同的选择器/ DOM结构,并测量它们的相对执行时间,以更好地了解哪些选择器实际上可感知地影响Web应用程序的性能。

Hope that helps. 希望有所帮助。

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

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