DOM事件监听器垃圾回收 [英] DOM event-listeners garbage collection

查看:158
本文介绍了DOM事件监听器垃圾回收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将事件侦听器附加到表的特定列中的每个单元格上,然后从表中删除每一行,为避免内存泄漏,开发人员必须在行被删除之前删除事件侦听器

If an event listener is attached to every cell in a particular column of a table, and then every row is removed from the table, to avoid memory leaks is it necessary for the developer to remove the event-listeners before the rows are deleted, or do the browsers clean things up?

编辑:Michael建议的文章说事件委托的效果更好而不是将侦听器直接绑定到表中的每个单元格,但是我不确定从垃圾回收角度看它是否更好,还是仅表现得更好。请评论。摘自文章:

the article that Michael suggested says event delegation performs better than binding the listener directly to every cell in the table, but I am not sure if it is better from a garbage-collection perspective or just performs better. Please comment. From the article:

        $('table').on('click', 'td', function () {

            $(this).toggleClass('active');

        });

优于:

        $('table td').on('click', function () {

            $(this).toggleClass('active');

        });

Edit2:和。on()和事件委托的jQuery文档也着重于性能,但问题是当从行中反复删除行时,从垃圾回收的角度来看会发生什么 em>从一个大表中,并保留委托的机制正在听单元格单击。

and the jQuery documentation of .on() and event delegation also focuses on performance, but the question of what happens from a garbage-collection perspective when rows are repeatedly deleted en masse from a large table, and the cell-click is being listened to by the delegated mechanism, remains.

推荐答案

我是不确定,但是我想这篇出色的文章会整理(搜索垃圾回收): http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficiency-javascript/

I am not sure, but i guess this brilliant article will clean things up (search for "Garbage Collection"): http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/

这篇关于DOM事件监听器垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆