如果使用自定义属性选择器而不是使用 jQuery 的长混合类/标签选择器,性能会有所提高吗?

发布于 2024-12-13 12:06:27 字数 552 浏览 3 评论 0原文

我注意到我们团队中的一些开发人员正在创建长 jQuery 选择器,例如:

'div.someclass > span.someotherclass ...'

我知道,jQuery 将尝试对类和标签使用本机 DOM 方法,但我认为使用精简版的东西是否会更有效:

'[data-gid="my-element-group"]'

而不是那些长的混合类/标签选择器。

自定义属性选择器会比长类/标签选择器更快吗?还是取决于具体情况?为 Javascript 密集的网站选择哪种方法?

PS 我想,如果 HTML 设计者和 Javascript 编码器分开工作,那些长选择器也是危险的 - 设计者可能仅仅通过更改 CSS 类或移动标签来破坏 Javascript 功能。但如果他们看到 ids 和 data-gids,他们可以不理会它们,这应该更安全......但它会牺牲性能吗?

PPS 我不关心无效的 HTML4,因为自定义属性,性能对于我的大多数客户来说更为重要。

I noticed that some of developers in our team are creating long jQuery selectors like:

'div.someclass > span.someotherclass ...'

I know, that jQuery will try to use native DOM methods for classes and tags, but I think if it would be more efficient to use something lite this:

'[data-gid="my-element-group"]'

instead of those long mixed class/tag selectors.

Will custom attribute selectors be faster than long class/tag selectors or it depends on circumstances? Which approach to choose for a Javascript-heavy website?

P.S. I guess, those long selectors are also dangerous if HTML designers and Javascript coders are working separately - designer may break Javascript functionality just by changing CSS classes or moving tags around. But if they see ids and data-gids, they can leave them alone, and that should be much safer... but will it sacrifice performance?

P.P.S. I don't care about invalid HTML4 because of custom attributes, the performance is much more important for most of my clients.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-12-20 12:06:27

选择器越长应该越快。这是一个例子。给定以下 HTML:

<div class="testDiv">
    <span class="testSpan"></span>
</div>
<div>
    <span data-group="testSpan"></span>
</div>

和以下 jQuery:

var elems1 = $("div.testDiv > span.testSpan");
var elems2 = $("[data-group='testSpan']");

这是快速测试的结果:

在此处输入图像描述

但是,值得注意的是,在现实世界中,差异很小,您应该使用其中的一个最适合你。

您可以通过使其更具体来使您的 data-* 选择器稍微好一些(性能方面):

var elems = $("span[data-group='testSpan']");

但另一个快速测试表明(至少在 Chrome 15 中对我来说),较长的选择器仍然获胜。

我认为在不支持 querySelectorAll 的旧浏览器中,差异会更加明显,有利于较长的选择器,它可以使用本机 getElementsByTagName

The longer selectors should be faster. Here's an example. Given the following HTML:

<div class="testDiv">
    <span class="testSpan"></span>
</div>
<div>
    <span data-group="testSpan"></span>
</div>

And the following jQuery:

var elems1 = $("div.testDiv > span.testSpan");
var elems2 = $("[data-group='testSpan']");

Here's the results from a quick test:

enter image description here

However, it's worth noting that in the real world the difference is going to be minimal and you should use whichever suits you best.

You could make your data-* selector slightly better (performance-wise) by making it more specific:

var elems = $("span[data-group='testSpan']");

But another quick test reveals (for me, in Chrome 15 at least) that the longer selectors still win.

I think in older browsers with no support for querySelectorAll, the difference will be even more pronounced in favour of the longer selectors, which can use the native getElementsByTagName.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文