Parent(),更快的替代方案?

发布于 2024-12-25 14:28:00 字数 959 浏览 3 评论 0原文

我正在使用 div 仪表板,每个 div 都有一个包含按钮的树。每次我都必须知道该 div 的 id 是什么,所以我经常使用parent()。

大多数情况下,我正在执行 $(this).parent().parent().parent() 来查找 div 的 ID,以便我可以为其设置变量。该应用程序基于每个 div 的 id。

使用parent()最多3次但几乎每个函数都使用很慢吗?

还有其他选择吗?

我正在寻找类似基准样式的东西来显示什么更快。

这是树的示例:

<div id="6179827893" class="dashdiv">
   <div class="buttons">
     <li><a href="#" class="btn1">Button 1</a></li>
     <li><a href="#" class="btn2">Button 2</a></li>
     <li><a href="#" class="btn3">Button 3</a></li>
     <li><a href="#" class="btn4">Button 4</a></li>
     <li><a href="#" class="btn5">Button 5</a></li>
     <li><a href="#" class="btn6">Button 6</a></li>
   </div>
   <div class="dashcontent">

    ....

   </div>
</div>

I am working with a dashboard of divs and each div it has a tree of which the buttons are. Every time I have to know which the id of that div is so I am using parent() alot.

Mostly I am doing $(this).parent().parent().parent() to find the ID of div so I can set variables to it. The app is based on the id's of each div.

Is it consider slow to use parent() up to 3 times but pretty much on every function?

Is there any other alternative?

I am looking for something like benchmarks-style which shows what's faster.

Here is an example of the tree:

<div id="6179827893" class="dashdiv">
   <div class="buttons">
     <li><a href="#" class="btn1">Button 1</a></li>
     <li><a href="#" class="btn2">Button 2</a></li>
     <li><a href="#" class="btn3">Button 3</a></li>
     <li><a href="#" class="btn4">Button 4</a></li>
     <li><a href="#" class="btn5">Button 5</a></li>
     <li><a href="#" class="btn6">Button 6</a></li>
   </div>
   <div class="dashcontent">

    ....

   </div>
</div>

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

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

发布评论

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

评论(4

ゃ人海孤独症 2025-01-01 14:28:00

您有几种选择可以达到相同的效果。

基准:http://jsperf.com/parents-method。根据此基准测试,我的方法比您的方法大约快 100 倍。

Method (see below) : Operations per second (higher is better)
parentNode3x       : 4447k
$(parentNode3x)    :  204K
$().closest        :   35k
$().parents        :    9k
$().parent()3x     :   44k

// Likely the fastest way, because no overhead of jQuery is involved.
var id = this.parentNode.parentNode.parentNode.id;

// Alternative methods to select the 3rd parent:
$(this.parentNode.parentNode.parentNode) // Native DOM, wrapped in jQuery

// Slowpokes
$(this).closest('.dashdiv')              // Hmm.
$(this).parents('.dashdiv:first')        // Hmm...

You've got a few options to achieve the same effect.

Benchmark: http://jsperf.com/parents-method. According to this benchmark, my method is roughly 100x faster than your method.

Method (see below) : Operations per second (higher is better)
parentNode3x       : 4447k
$(parentNode3x)    :  204K
$().closest        :   35k
$().parents        :    9k
$().parent()3x     :   44k

// Likely the fastest way, because no overhead of jQuery is involved.
var id = this.parentNode.parentNode.parentNode.id;

// Alternative methods to select the 3rd parent:
$(this.parentNode.parentNode.parentNode) // Native DOM, wrapped in jQuery

// Slowpokes
$(this).closest('.dashdiv')              // Hmm.
$(this).parents('.dashdiv:first')        // Hmm...
太阳男子 2025-01-01 14:28:00

您可能最好使用 .closest(),如下所示:< code>$(this).closest('.dashdiv')

从引擎的角度来看,它并没有更快,因为您仍然在 DOM 层中循环,但对于新手来说也更清楚作为较短的代码。

注释

如果您追求的是纯粹的速度,您不妨完全跳过 jQuery 并使用 node.parentNode 代替。但这涉及到计数周期的棘手问题,我认为这是一个学术问题。

如果您正在为主要生产编写高性能代码,例如商业搜索引擎或网络邮件提供商,那么计算周期就很重要,因为任何小的优化都会成倍增加数千倍。恕我直言,我怀疑您正在编写这种代码。

如果你正在写的东西最多一次会被几个人点击,那么小的优化是一种智力练习,不会以任何明显的方式影响结果。在任何用户开始注意到之前,您必须将代码的效率提高数百毫秒,而此代码不会这样做。

相反,更重要的是要考虑下一个将查看您的代码的开发人员。对于开发人员来说,拥有清晰、编写良好的代码来立即传达其正在执行的操作非常重要。像 parent().parent().parent() 这样令人眼花缭乱的方法链可能会让其他开发人员感到困惑和困惑,更不用说 node.parentNode.parentNode.parentNode

-- 这就是首先创建 .closest() 的原因。它清晰、简洁,而且效率并不比它所取代的链条低。千分之九百九十九,这就是你应该走的路。

You might be better off using .closest(), like this: $(this).closest('.dashdiv')

It's not any faster from an engine's point of view, since you're still looping up through the DOM layers, but it's more clear to a newcomer as well as shorter code.

COMMENTARY

If it's pure speed you're after, you might as well skip jQuery entirely and use node.parentNode instead. But this is getting into niggly issues of counting cycles, and I think it's an academic one.

If you're writing high-performance code for major production, like a commercial search engine or webmail provider, then counting cycles is important because any small optimization gets multiplied thousands of times. With all due respect, I doubt you're writing that kind of code.

If you're writing something that's going to be hit by a few people at a time, at most, then small optimizations are an intellectual exercise that won't affect results in any noticable way. You'd have to improve your code's efficiency by hundreds of milliseconds before any user would even begin to notice, and this code isn't going to do that.

Instead, it's far more important to think about the next developer who will be looking at your code. For that developer, it's important to have clear, well-written code that immediately communicates what it's doing. Eye-blurring chains of methods like parent().parent().parent() can obscure and confuse other developers, to say nothing of node.parentNode.parentNode.parentNode

-- which is why .closest() was created in the first place. It's clear, concise, and not noticably less efficient than the chains it replaces. 999 times out of a thousand, it's the way you ought to go.

枫以 2025-01-01 14:28:00

首先,不要过早优化。如果它没有引起问题(并通过各种方式在一系列平台上进行彻底测试),那么就不用担心。

有一个可能的优化:使用原生 DOM 属性:

var id = this.parentNode.parentNode.parentNode.id;

请注意,最好的 jQuery 方法(速度会更慢,但这可能不是问题)是使用 closest

$(this).closest('div.dashdiv').prop('id');

First, don't optimise prematurely. If it's not causing a problem (and test thoroughly by all means, across a range of platforms) then don't worry about it.

There is a possible optimisation: use native DOM properties:

var id = this.parentNode.parentNode.parentNode.id;

Note that the nicest jQuery way to do this (which will be slower, but that may not be a problem) is with closest:

$(this).closest('div.dashdiv').prop('id');
旧人 2025-01-01 14:28:00

如果处理程序当前位于 元素上,请将它们放在 .dashdiv 元素上。

如果 e.target 元素,那么您可以执行 this.id

$('.dashdiv').click(function(e) {
    if( e.target.nodeName.toLowerCase() === 'a' ) {
        alert( this.id );
    }
});

If the handlers are currently on the <a> elements, place them on the .dashdiv elements instead.

Then you can do this.id if the e.target was an <a> element.

$('.dashdiv').click(function(e) {
    if( e.target.nodeName.toLowerCase() === 'a' ) {
        alert( this.id );
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文