JQuery 代码中使用的 CSS 类的事件侦听器的性能开销

发布于 2024-07-27 15:08:44 字数 459 浏览 3 评论 0原文

如果我的标记如下所示:

<button id="button-1" class="nice">
<button id="button-2" class="nice">
<button id="button-3" class="nice">

并且我定义了以下 jQuery:

$(".nice").click(function () { 
    // something happens
});
  1. 建立了多少个事件侦听器? 1 还是 3?

  2. 如果我有 1000 个按钮而不是 3 个,我应该改用事件委托吗?

  3. 从性能角度来看,如果元素类在标记中包含大量元素,最好不要在这些类上定义 jQuery 调用吗?

    从性能角度来看,

If my markup looks like this:

<button id="button-1" class="nice">
<button id="button-2" class="nice">
<button id="button-3" class="nice">

and I define the following jQuery:

$(".nice").click(function () { 
    // something happens
});
  1. How many event listeners are established? 1 or 3?

  2. If I have 1000 buttons instead of 3, should I use event delegation instead?

  3. Is it best, performance-wise, to not define jQuery calls on classes of elements if those classes contain a large number of elements in the markup?

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

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

发布评论

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

评论(4

鸵鸟症 2024-08-03 15:08:44

1) 3 个事件监听器都指向同一个函数。

2) 这取决于您想要完成的任务

3) 尝试做一些基准测试,看看性能影响在哪里变得明显。 我选择了多达 1000 个元素,然后同时进行动画处理 - 并且(在 FF 中)直到大约 600-700 个元素时,性能影响才明显。 性能当然取决于浏览器和JS引擎。 有些(Chrome)会比其他(IE)更快。 值得一提的是,对于该网站,我使用了与您问题中相同的方法。

1) 3 event listeners all pointing to the same function.

2) That depends on what you're trying to accomplish

3) Try doing some benchmarks, see where the performance hit becomes noticeable. I've had up to 1000 elements being selected then animated at the same time - and (in FF) the performance hit was unnoticeable up until about 600-700 elements. Performance of course depends on browser and JS engine. Some (Chrome) will be faster than others (IE). Its worth mentioning that for that site I used the same method as you have in your question.

天生の放荡 2024-08-03 15:08:44

1) 3

2) 是的

3) 是的,正如您所说,使用事件委托,在 jQuery 中可以通过 .live 功能。 请注意,太多 .live 函数也会影响性能,因此请考虑本机事件委托。

1) 3

2) Yes

3) Yes, as you have said use event delegation which in jQuery is available through the .live function. Be carefull, too many .live function can also impact performance so do consider native event delegation.

謌踐踏愛綪 2024-08-03 15:08:44
  1. 是的,因为 $() 函数返回一个数组,并且 click() 应用于其中的所有元素。
  2. 是的。 当您有大量可以触发事件的节点,或者当这些节点的数量可以改变时,委派通常会获得最佳回报。 这使得您只需初始化事件侦听器一次,无论 DOM 发生什么情况。 如果您计划拥有超过 100 个节点,我会使用事件委托而不是事件处理。
  3. 是的。 这将改善您的响应时间(尤其是对于非常大的列表[请进行基准测试]),并使您的初始化时间复杂度为 O(1) 而不是 O(n)。
  1. Yes, because the $() function returns an array and click() is applied to all of the elements therein.
  2. Yes. Delegation usually pays off best when you have a large number of nodes that can trigger an event, or when the quantity of those nodes can change. This allows you to only have to initialize your event listener once, no matter what happens to the DOM. If you plan on having more than 100 nodes, I would use event delegation over event handling.
  3. Yes. This will improve both your response time (most notably for extremely large lists [please benchmark]), as well as make your initialization O(1) rather than O(n).
白昼 2024-08-03 15:08:44

完全同意! 您必须向包含所有这些好元素的父元素添加一个事件侦听器。 这将大大提高性能。

Completely agree! You must add an event listener to the parent element containing all those nice elements. That will improve the performance a lot.

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