jQuery的性能 - 由data-attr或类选择? [英] jQuery performance - select by data-attr or by class?

查看:59
本文介绍了jQuery的性能 - 由data-attr或类选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更快,为什么?通过 $('div [data-something]')选择div(插件需求) $('div.something')?我倾向于前者,因为它是更干净。



根据这个SO问题我知道我不应该同时使用这两个。但是我没有发现这些之间是否有区别。

解决方案

浏览器会有所不同。现在几乎所有的浏览器都支持 querySelectorAll ,jQuery将可以使用它。 querySelectorAll 可以与属性存在选择器一起使用,所以如果有jQuery没有必要的工作,它可以将其卸载到引擎。



对于没有 querySelectorAll 的旧浏览器,jQuery显然必须做更多的工作,但即使是IE8也可以。



与大多数这些事情一样,你最好的选择是:


  1. 不用担心直到/除非您看到问题,


  2. 如果您看到问题,请在您打算支持的浏览器上进行配置,然后做出明智的决定。



Which is faster and why? Selecting div (for plugin needs) by $('div[data-something]') or $('div.something')? I lean towards the former since it's "cleaner".

Based on this SO question I know I shouldn't be using both. However I didn't find out whether there is a difference between these.

解决方案

It will vary by browser. Nearly all browsers now support querySelectorAll, and jQuery will use it when it can. querySelectorAll can be used with attribute presence selectors, so if it's there jQuery doesn't have to do the work, it can offload it to the engine.

For older browsers without querySelectorAll, jQuery will obviously have to do more work, but even IE8 has it.

As with most of these things, your best bet is:

  1. Don't worry about it until/unless you see a problem, and

  2. If you see a problem, profile it on the browsers you intend to support and then make an informed decision.

这篇关于jQuery的性能 - 由data-attr或类选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆