jQuery:什么更高效?许多ID特定选择器,或一个“包含前缀选择器”可以被选择。
我在一个项目中有一段 JavaScript 代码片段,它在仪表板上缓存了一组 jQuery 对象(锚标记):
$.extend(cache, {
dashboard : {
buttons : [
$('#dash-new-lead'), $('#dash-jobs-calendar'),
$('#dash-view-lead'), $('#dash-sales-reports'),
$('#dash-search-leads'), $('#dash-new-job'),
$('#dash-dispatch-jobs'), $('#dash-dispatch-reports'),
$('#dash-manage-employees'), $('#dash-manage-trucks'),
$('#dash-finalize-jobs'), $('#dash-payment-profiles'),
$('#dash-employee-statements'), $('#dash-company-statements'),
$('#dash-finance-reports'), $('#dash-admin-sources'),
$('#dash-admin-statuses'), $('#dash-admin-companies'),
$('#dash-admin-groups'), $('#dash-admin-users'),
$('#dash-admin-dispositions'), $('#dash-search-jobs'),
$('#dash-jobs-calendar-dispatch'), $('#dash-new-lead-dispatch'),
$('#dash-finance-notices')
]
}
});
每个链接稍后都会设置样式(使用 $.each
)作为按钮。每个链接都有一个唯一的 jQuery UI 图标(因此 id 而不仅仅是类选择器)。根据用户的访问级别,某些链接可能存在也可能不存在于 DOM 中。
我现在想知道使用 jQuery 的 包含前缀选择器:
$.extend(cache, {
dashboard : {
buttons : $('a[id|="dash-"]')
}
});
- 优点:更少引用 jQuery 对象 = 更快
- 缺点:jQuery 无法使用
document.getElementByID
= 更慢
I have a snippet of JavaScript in a project that caches an array of jQuery objects (anchor tags) on a dashboard:
$.extend(cache, {
dashboard : {
buttons : [
$('#dash-new-lead'), $('#dash-jobs-calendar'),
$('#dash-view-lead'), $('#dash-sales-reports'),
$('#dash-search-leads'), $('#dash-new-job'),
$('#dash-dispatch-jobs'), $('#dash-dispatch-reports'),
$('#dash-manage-employees'), $('#dash-manage-trucks'),
$('#dash-finalize-jobs'), $('#dash-payment-profiles'),
$('#dash-employee-statements'), $('#dash-company-statements'),
$('#dash-finance-reports'), $('#dash-admin-sources'),
$('#dash-admin-statuses'), $('#dash-admin-companies'),
$('#dash-admin-groups'), $('#dash-admin-users'),
$('#dash-admin-dispositions'), $('#dash-search-jobs'),
$('#dash-jobs-calendar-dispatch'), $('#dash-new-lead-dispatch'),
$('#dash-finance-notices')
]
}
});
Each link is styled later (using $.each
) as a button. Each link gets a unique jQuery UI icon (hence the id's instead of just a class selector). Depending on a user's access level some links may or may not exist in the DOM.
I'm wondering now if it would be more efficient to use jQuery's Contains Prefix Selector:
$.extend(cache, {
dashboard : {
buttons : $('a[id|="dash-"]')
}
});
- Pro: Fewer references to the jQuery Object = faster
- Con: jQuery cannot use
document.getElementByID
= slower
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建一个包含所有标识的选择器:
不过,我会考虑向按钮添加一个类,以便使用简单的选择器轻松定位它们。这将使维护代码变得更容易。
You can make a single selector that contains all the identities:
I would however consider adding a class to the buttons so that it's easy to target them using a simple selector. That would make it easier to maintain the code.
这在很大程度上取决于浏览器,因此您需要进行测试才能确定。
因为
'a[id|="dash"]'
(注意我删除了-
) 似乎是有效的querySelectorAll( )
选择器,任何支持该方法的浏览器(包括 IE8)都应该具有非常好的性能。因为您包含
tagName
,所以不支持querySelectorAll()
的浏览器将(我相信)使用getElementsByTagName()
,因此过滤器只会应用于它找到的元素。
如果您能够将查询限制在比
文档
更窄的上下文中,它也会有所帮助。另请注意,根据此测试,
getElementById()
的性能在 IE6 和 IE7 中非常令人兴奋。 IE8 确实支持querySelectorAll()
,不过确保它在特定选择器上成功也不错。如果没有,查询将默认使用 Sizzle 的引擎。这里有一个测试,直接通过
querySelectorAll()
使用该选择器。您可以在不同的浏览器中运行它以查看哪些地方支持它。另请注意,IE9 之前不支持
getElementsByClassName()
,因此我怀疑使用类是否会比'a[id|="dash"]'
提供任何巨大的性能改进代码>. Firefox3 是个例外,它支持getElementsByClassName()
,但不支持querySelectorAll()
。It'll depend very much on the browser, so you'll need to test to be sure.
Because
'a[id|="dash"]'
(notice I removed the-
) appears to be a validquerySelectorAll()
selector, any browsers that support that method (which includes IE8) should have very good performance.Because you're including the
tagName
, browsers that don't supportquerySelectorAll()
will (I believe) usegetElementsByTagName()
, so the filter will only be applied to the<a>
elements it finds.It should help as well if you're able to limit the query to a narrower context than the
document
.Note also that according to this test, the performance of
getElementById()
isn't terribly exciting in IE6 and IE7. And again IE8 does supportquerySelectorAll()
, though it wouldn't be bad to make sure it succeeds with that particular selector. If not, the query will default to Sizzle's engine.Here's a test using that selector directly through
querySelectorAll()
. You can run it in different browsers to see where it is supported.Note also that
getElementsByClassName()
isn't supported before IE9, so I doubt that using a class will offer any great performance improvements over'a[id|="dash"]'
. The exception being Firefox3, which does supportgetElementsByClassName()
, and notquerySelectorAll()
.除了每个元素都有其唯一的 ID(
dash-new-jobs
等)之外,还为其指定一个类 (class='dash-button'
)。然后,您可以使用例如有效地引用整个集合
,如果您需要像您提到的那样给它们提供所有不同的图标,请使用它们唯一的ID:
高效(尽管正如其他人所说,如果您不确定,请进行性能测试!),和干净/易于理解的代码(比使用“包含”选择器干净得多!)。
As well as each of your elements having its unique id (
dash-new-jobs
etc), give it a class too (class='dash-button'
).You can then refer to the whole collection efficiently using e.g.
And if you need to give them all different icons like you mentioned, use their unique ID:
Efficient (though as the others have said, do performance tests if you're unsure!), and clean/easy to understand code (much cleaner than using 'contains' selector!).
我建议您使用 contains 前缀方法,因为它更容易维护。您以这种方式进行设置,您不再需要担心要选择哪些 id,您只需定位那里的组即可。
虽然 id 方法可能具有更好的性能,但您要为每个方法创建一个 jQuery 对象。这也一定有影响。
I'd suggest you go with the contains prefix method because it's easier to maintain. You set it up this way and you no longer need to worry about which ids to select, you just target the group that's there and you're done.
While the id method will likely have better performance, you're creating a jQuery object for each one. That's got to have an impact too.