简体   繁体   English

使用jQuery提高选择器性能

[英]Improve selectors performance with jQuery

I have been taking a look on some sites and they all talk about using tag selectors instead of classes to improve the performance. 我一直在看一些站点,他们都在谈论使用标记选择器而不是类来提高性能。

For example, this: 例如,这:

$("input.myclass");

Instead of this: 代替这个:

$(".myclass");

For example: 例如:

They all claimed that JavaScript only had getElementById and getElementsbyTagName and not a way to select classes directly. 他们都声称JavaScript仅具有getElementByIdgetElementsbyTagName而不能直接选择类。

Has this changed in the last 3 years? 过去3年有没有改变? Are they now able to select by class? 他们现在可以按班级进行选择了吗? I was testing it with jsperf and it seems the class selector is faster by far: http://jsperf.com/class-vs-input 我正在用jsperf进行测试,看来到目前为止,类选择器的速度更快: http ://jsperf.com/class-vs-input

I also took a look at other's people testings and show the same results: http://jsperf.com/selectors-perf/3 我还查看了其他人的测试并显示了相同的结果: http : //jsperf.com/selectors-perf/3

Has this changed in these last year? 去年这些改变了吗? Should we now select by class rather than by tags? 我们现在应该按类而不是按标签选择吗? Where can I take a look at the browsers' versions implementing natively the class selector? 在哪里可以看到本机实现类选择器的浏览器版本?

Thanks. 谢谢。

It has changed now. 现在已经改变了。

Most of the browsers are implementing : 大多数浏览器正在实现:

var matches = document.body.querySelectorAll('div.highlighted > p');

Inside their implementation in javascript. 在javascript中实现它们。

That's what jQuery uses inside now; 那就是jQuery现在使用的东西; It is implementing sizzle.js, a selector js library that chooses whether to use querySelector or the regular getElementsByTagName function; 它正在实现sizzle.js,这是一个选择器js库,用于选择是使用querySelector还是常规的getElementsByTagName函数。

For example, for the jquery constructor function $() if the first argumemt is a string : $(iAmAString) , then if the first letter of the string is a # , jquery will call document.getElementById(iAmAString.substr(0)) . 例如,对于jquery构造函数$()如果第一个argumemt是字符串: $(iAmAString) ,则如果字符串的第一个字母是# ,则jquery将调用document.getElementById(iAmAString.substr(0)) If not it will let sizzle handle whether calling querySelector depending on the browser's compatibility and the complexity of the string. 如果不是,它将让sizzle处理是否根据浏览器的兼容性和字符串的复杂性调用querySelector。

and lots of other awesome things. 还有很多其他很棒的东西。

Being the most precise when selecting your element, using base functions and caching your frequently used selectors, will reduce the number of checks when passing through this big chain and/or even circumvent the whole chain. 选择基本元素,使用基本函数并缓存常用的选择器时,最精确的做法将减少通过该大链和/或绕开整个链时的检查次数。

For some websites, this had the particular effect of generating a unicorn riding kitten effect of awesomeness, what more to say: 对于某些网站,这具有产生令人敬畏的独角兽骑行小猫效果的特殊效果,还需说明:

the compatibiity support is here https://developer.mozilla.org/en-US/docs/Web/API/Element.querySelectorAll 兼容性支持在这里https://developer.mozilla.org/en-US/docs/Web/API/Element.querySelectorAll

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

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