简体   繁体   English

数据属性css选择器比类选择器更快吗?

[英]Are data attribute css selectors faster than class selectors?

A few months ago this article pointed out that classes could actually be avoided all together from website development. 几个月前, 这篇文章指出,实际上可以通过网站开发来避免课程。

My question is, how efficient are the data selectors compared to class selectors? 我的问题是,与类选择器相比,数据选择器的效率如何?

A simple example would be to compare querying for elements with data-component='something' versus elements with class='class1 class2 something anotherClass' . 一个简单的例子是将元素的查询与data-component='something'进行比较,将元素与class='class1 class2 something anotherClass'

The [data-<attr>='<value>'] selector will check the value as a whole versus the class string that should be split. [data-<attr>='<value>']选择器将检查整个值与应拆分的类字符串。 With this in mind, data atributes should be faster. 考虑到这一点,数据属性应该更快。

So, to refine the question, in the case of CSS, are we better off with class selector or data selector? 那么,为了改进这个问题,在CSS的情况下,我们最好使用类选择器还是数据选择器? And from a javascript point of view, will jQuery("[data-component='something']") be more efficient than jQuery(".something") ? 从javascript的角度来看, jQuery("[data-component='something']")会比jQuery(".something")更有效吗?

I wouldn't call it conclusive , but it does appear class selectors are faster... I just put this together for a quickie test. 我不会把它称为决定性的 ,但它确实会出现类选择器更快...我只是将它们放在一起进行快速测试。

http://jsperf.com/data-selector-performance http://jsperf.com/data-selector-performance

EDIT : 编辑

Based on Vlad's and my jsperf tests... if performance is a concern (especially IE)... classes are still the way to go 基于弗拉德和我的jsperf测试...如果性能是一个问题(特别是IE)...类仍然是要走的路

After checking out BLSully's answer and the test page he provided, here are the actual figures for comparison. 在查看BLSully的答案和他提供的测试页后 ,这里是实际数据进行比较。

jQuery class selector performance vs jQuery data attribute selector performance operations per second: jQuery类选择器性能与每秒jQuery数据属性选择器性能操作:

  • 31% faster in Chrome 27.0.1453 Chrome 27.0.1453的速度提高了31%
  • 140% faster in Firefox 15.0.1 Firefox 15.0.1快140%
  • 131% faster in Firefox 21.0 Firefox 21.0的速度提高131%
  • 1,267% faster in IE 9.0 IE 9.0的速度提高了1,267%
  • 1,356% faster in IE 10.0 IE 10.0中快了1,356%

I have the impression that the performance of the selectors are fast enough right now even in the mobile browsers out there. 我的印象是即使在那里的移动浏览器中,选择器的性能也足够快。 Unless you really plan to use selectors a lot, data-attributes or class based, (in which case I would suggest to revisit your code to try to cache the already queried selectors) we can consider them not that bad. 除非你真的打算大量使用选择器,数据属性或类,(在这种情况下我建议重新访问你的代码以尝试缓存已经查询的选择器),我们可以认为它们并不那么糟糕。 And I would even say that is not dramatic to use style over the others. 而且我甚至会说使用风格而不是其他风格并不显着。

I think browsers vendors have spent more time improving the most used scenario (query against classes) than querying against selectors. 我认为浏览器供应商花了更多的时间来改进最常用的场景(针对类的查询),而不是查询选择器。 This is changing and I would not be surprised if they start optimizing other cases too. 这种情况正在发生变化,如果他们也开始优化其他案例,我也不会感到惊讶。

I don't know about all this answers but i ran my own test and attribute are faster. 我不知道所有这些答案,但我运行自己的测试和属性更快。

you can try it your self just save the time at the beginning, do the task and get the final time then do the math 你可以尝试自己,只需在开始时节省时间,完成任务并获得最后时间,然后进行数学运算

  var newDate = new Date().getTime(); $('.test-t').removeClass('act'); $('.t1r').addClass('act'); var currentDate = new Date().getTime(); var diff = currentDate-newDate; console.log(diff); var newDate = new Date().getTime(); $('.test-t2').attr('this-act',''); $('.t2r').attr('this-act','1'); var currentDate = new Date().getTime(); var diff = currentDate-newDate; console.log(diff); 

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

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