简体   繁体   English

JQuery 1.3+选择器性能

[英]JQuery 1.3+ Selector Performance

I've just created a jquery selector performance testing page. 我刚刚创建了一个jQuery选择器性能测试页面。 http://guhelouis.github.com/jquery_performance/ http://guhelouis.github.com/jquery_performance/

There are 10000 divs with class name from .div-cl-1 to .div-cl-10000. 有10000个div,其类名称从.div-cl-1到.div-cl-10000。 And I'm trying to get the .div-cl-9999 with 2 different selector : 我正在尝试使用2个不同的选择器来获取.div-cl-9999:

  • $('div.div-cl-9999) $('div.div-CL-9999)
  • $('.div-cl-9999') $(” DIV-CL-9999' )

As you may guess that the first one will be faster than the second one. 您可能会猜到第一个比第二个要快。 But, please see the result in http://guhelouis.github.com/jquery_performance/ (push the Run it button, sorry but it's a little bit slow), the first result will always be the first one is slower than the second one. 但是,请在http://guhelouis.github.com/jquery_performance/中查看结果(按“ 运行它”按钮,对不起,但是有点慢),第一个结果将始终是第一个结果要慢于第二个结果。

and if you reverse the order in running, means : 如果您颠倒了运行顺序,则表示:

  • $('.div-cl-9999') $(” DIV-CL-9999' )
  • $('div.div-cl-9999) $('div.div-CL-9999)

The result is back to normal again. 结果再次恢复正常。

Is it some kind of 'indexing' inside the new jquery selector engine? 新的jquery选择器引擎内部是否存在某种“索引编制”功能?

PS: these weird result occurs only using jquery 1.3+ (the time JQuery started to use Sizzle). PS:这些奇怪的结果仅在使用jQuery 1.3+(JQuery开始使用Sizzle的时间)时发生。

Both are different selectors. 两者都是不同的选择器。

$('div.div-cl-1') // select all div elements with class "div-cl-1" 

$('.div-cl-1') // select all elements with class "div-cl-1"

For eg : The first one will not select something like 例如:第一个不会选择类似

<p>
    <span class="div-cl-1">test</span>
</p>

EDIT: Just found this - http://jsperf.com/jquery-selector-speed-tests 编辑:刚发现这个-http: //jsperf.com/jquery-selector-speed-tests

I am not sure about jQuery, but css selection works faster if you don't specify the tag name of the specified class, because css first looks at the class name and then checks to see if the element is the specified tag. 我不确定jQuery,但是如果您不指定指定类的标记名,则选择css的速度会更快,因为css首先查看类名,然后检查该元素是否为指定的标记。 Assuming that jQuery uses native query selector of the browser or class selector (if it exists) the first selector will be slower. 假设jQuery使用浏览器的本机查询选择器或类选择器(如果存在),则第一个选择器将变慢。

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

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