javascript - JS events listeners memory consumption
问题描述
Let's say I need to add the same event listener on each one of the following nodes:
function handler(e) { ... }
var clickMe = document.querySelectorAll('.click-me');
for (var i = 0; i < clickMe.length; i++) {
clickMe[i].addEventListener('click', handler);
}
Will the memory grow in this case? If the answer is yes, why? I'm using the same function reference in each one of them. Is it because under the hood it does something like this:
class Div {
listeners = {
click: []
}
addEventListener(name, handler) {
this.listeners[name].push(handler)
}
}
And it causes the array to be bigger?
I know that we should use event delegation, but that's not my question. The question is, why in this case? (I don't talk about the dynamic DOM nodes that could be added over time, only about memory)
解决方案
推荐阅读
- firebase - 如何在 Firebase 服务器上上传资产包?google 提供服务器上传可用于android / IOS 平台游戏的资产包?
- jquery - iOS 浏览器中未显示画布外菜单
- shell - 当命令有更改用户的命令时如何获取ssh的输出
- yammer - yammer 最近的更新降级了消息 API
- hive - 如何“过滤” Hive 表中的记录?
- kubernetes - 为什么即使禁用交换后 kubeadm 也无法启动?
- mysql - 选择距当前时间 2 小时内的行
- python - 无法安装超级账本 indy-node 的开发设置
- c++ - 如何使用 OpenCV 拉直曲线?
- c# - 获取指定用户对表的列权限