.on() 和 .click() 哪个更快?

发布于 2025-01-03 04:00:25 字数 238 浏览 0 评论 0原文

如果我有一个带有删除记录链接的表,那么连接单击事件的最佳方法是什么?这些在幕后的评价是否相同?

$("#table").on("click", ".delete", function(){
   //do stuff
});

或者

$("#table .delete").click(function(){
   //do stuff
});

If I have a table with links to delete a record, which is the best way to wire up the click event? Do these evaluate the same under the hood?

$("#table").on("click", ".delete", function(){
   //do stuff
});

or

$("#table .delete").click(function(){
   //do stuff
});

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

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

发布评论

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

评论(4

梦回梦里 2025-01-10 04:00:25

不,他们没有。

按照您使用它们的方式...

.on() 您使用它的方式(委托),将为您节省附件和内存的周期,因为它实际上并没有在那一刻附加事件,因为它提供了一个动态元素,其中事件也应用于未来的元素。

.click() 将节省执行时的周期,因为它会及时将事件显式附加到每个匹配的元素,但实际上,这将是我寻找的最后一个地方瓶颈。

在此处阅读一些事件委托测试...

附加事件处理程序的性能测试...

No, they don't.

In the way you are using them...

.on() The way you are using it (delegation), will save you cycles at attachment as well as memory, because it doesn't actually attach the events in that moment, as it provides a dynamic element where the event is applied to future elements as well.

.click() will save you cycles at execution, as it explicitly attaches an event to every matching element at that moment in time, but in reality, this would be the last place I'd look for a bottleneck.

Read some testing with event delegation here...

Performance testing of attaching event handlers...

黄昏下泛黄的笔记 2025-01-10 04:00:25

这就像问 C#intstring 哪个更快。每种类型都有其用途...
问题不在于哪个更快,而在于何时使用什么。

on 允许您将事件附加到动态添加的元素,而 click 则不然。

如果您没有“动态添加元素”,请使用 click,否则使用 on

It's like asking what is faster int or string in C#. Each type has it's uses...
It's not about which is faster, it's about what to use when.

on allows you to attach event to dynamically added elements while click does not.

if you don't have "dynamically added elements" use click, otherwise use on.

月下凄凉 2025-01-10 04:00:25

clickon 更快,因为它将事件直接附加到元素上,而 on 则将事件附加到 #table 并查找 .delete 元素来触发事件,因此在触发 click 事件之前会执行一些代码。

尽管您不会看到任何差异,但差异可以忽略不计。

如果您使用 on,则仅有一个事件处理程序就足以处理 #table 内任意数量的 .delete 元素,其中 >click 将附加到每个 .delete 元素,以便在内存利用率方面更好地 on

click is faster that on because it attaches the event directly on the element where as on is attaching the event on #table and looking for .delete element to trigger the event so there is some code execution involved before the click event is triggered.

The difference is negligible though you will not see any difference.

If you use on the only one event handler is enough to take care of any number of .delete elements inside the #table where as click will be attached to each of the .delete elements so on better in terms of memory utilization.

江挽川 2025-01-10 04:00:25

在这种特殊情况下,.click(...) 将(可能)变慢,因为它将需要迭代与选择器匹配的每一行和每个单元格,并将处理函数附加到每一个。在这种特殊情况下,on(...) 可能会更快,因为它将处理程序附加到表,而不是每个具有 .delete 的元素> 类。

话虽如此,在这两者之间进行选择时,您需要考虑一些事情:

  1. 您的选择器有多复杂?如果您正在执行 $('#rootItem').on('click', '.foo .bar.baz .bum', function() { /* do stuff*/ }),则此操作本质上会比 $('#rootItem').on('click', '.foo', function() { /* do stuff*/ }) 慢,因为选择器在以下方面更复杂前一种情况。在这种情况下,支付附件费用并使用 click(...) 来节省评估 #rootItem< 内每次点击的运行时成本可能更有意义。 /code> 来查看它是否与选择器 '.foo .bar.baz .bum' 匹配。像您这样的简单单类选择器('.delete')将表现得很好。
  2. 内容会动态变化吗?如果是这样,您将负责对每个动态添加的项目调用 click(...)。只要您的“外部”选择器(在本例中为 '#table')不指向,on(...) 函数就可以更好地处理这个问题at 将动态添加的元素。将'#table'替换为'.table',并动态添加一个新的表,其类为table,新添加的表将'在您最初调用 on(...) 时,不会附加事件处理程序。

In this particular scenario, the .click(...) will (likely) be slower, because it will need to iterate through each row and each cell that matches the selector, and attach the handler function to each one. The on(...), in this particular case, is likely to be faster, because it attaches the handler to the table, not to each element that has the .delete class.

That being said, you need to consider a few things when choosing between these two:

  1. How complex is your selector? If you are doing $('#rootItem').on('click', '.foo .bar.baz .bum', function() { /* do stuff*/ }), this will be inherently slower than $('#rootItem').on('click', '.foo', function() { /* do stuff*/ }) because the selector is more complex in the former case. In this kind of scenario, it might make more sense to pay the price on attachment and use click(...) to save the run-time cost of evaluating every click within #rootItem to see if it matches the selector '.foo .bar.baz .bum'. A simple single-class selector like you have ('.delete') will perform just fine.
  2. Will the contents be changing dynamically? If so, you'll be responsible for calling click(...) on each dynamically-added item. The on(...) function can handle this a little better, as long as your "outside" selector ('#table' in this case) doesn't point at elements that will be dynamically added. Replace '#table' with '.table', and dynamically add a new table with class table, and the newly-added table won't have event handlers attached to it from your initial call to on(...).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文