Javascript绑定会占用内存而不使用? [英] Do Javascript bindings take up memory while not in use?

查看:133
本文介绍了Javascript绑定会占用内存而不使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个日历,点击日历上的一天,就会运行一个功能。你可以在日历上每个月进行一次,随着时间的推移,它会生成几个月。由于日历上的每一天,显示与否,都是使用所有天的类绑定到一个事件,我担心建立成千上万的绑定数量。

I have a calendar that I've built, and on clicking a day on the calendar, a function is run. You can go month to month on the calendar, and it generates months as it goes. Since every day on the calendar, shown or not, is binded to an event using the class of all the "days," I'm worried about the number of "bindings" building up into the thousands.

//after a new month is generated, the days are rebound
TDs.off("mousedown");
TDs = $(".tableDay");
TDs.on("mousedown", TDmouseDown);

当我学习C#/ Monogame时,我学到了很快重新更新游戏元素的功能。所以,我想知道JavaScript是否以相同的方式工作。 Javascript引擎会重复检查每个单个事件的绑定,看是否已经发生?所以一个这样的结构:

While I was learning C#/Monogame, I learned about functions that repeat very quickly to update game elements. So, I was wondering if javascript works in the same manner. Does the Javascript engine repeatedly check every single event binding to see if it has occurred? So a structure something like this:

function repeat60timesPerSecond(){
 if(element1isClicked){ //blah }
 if(element2isClicked){ //blah }
 if(element3isClicked){ //blah }
}

或者是Javascript可以实际上只是在事件发生时触发函数?

Or is Javascript somehow able to actually just trigger the function when the event occurs?

简而言之:Javascript绑定仅占用内存通过现有的?

In short: Do Javascript bindings take up memory solely by existing?

目前为止我的(不确定)研究:

我有多次尝试自己回答这个问题。首先,我做了一个 jsperf test。除了测试中一致性的明显问题,测试没有实际测试这个问题。它主要测试如果解除绑定没有比解除绑定的东西更快。而不是实际绑定在创建后占用多少内存。我无法想出使用这种测试服务来测试的方法。

I have made several attempts at answering this question on my own. First, I did a jsperf test. Apart from the obvious problems with consistency in my test, the test didn't actually test this question. It primarily tested if unbinding nothing was faster than unbinding something. Rather than how much memory the actual bindings take up after creation. I couldn't come up with a way to test this using this testing service.

然后我点了一下,发现了很多有趣的东西,但没有什么直接回答这个问题。我确实遇到了这个答案,这表明在类似情况下使用事件容器的单个事件绑定。

I then googled around a bit, and found quite a bit of interesting stuff, but nothing directly answering this question in clear terms. I did come across this answer, which suggests using a single event binding of the event container in a similar situation.

更新:

发布后,我想到了一种可能的方式来测试本地JS:

Right after posting this, I thought of a possible way to test this with local JS:

function func(){
    console.log("test");
}
for(x=1;x<1000;x++){
    $('#parent').append("<div id='x"+x+"' class='child'></div>");
    $("#x"+x).on("mousedown", func);
}

console.time("timer");

for(i=1;i<1000000;i++){
    q = Math.sqrt(i);
    if(q % 1 == 0){
        q = 3;
    }
}

console.timeEnd("timer");

在播放之后(更改for循环是什么,更改两者的迭代次数循环等),似乎事件绑定占用了很少的内存。

After playing around with this(changing what the for loop does, changing the number of iterations on both for loops, etc.) it appears that event bindings take up a VERY small amount of memory.

推荐答案

是的,他们都占用记忆,但不是很多。只有一个函数对象。每个元素都有一个指向该对象的指针,所以它可能是每个元素4个字节。

Yes, they all take up memory, but not very much. There's just one function object. Each element has a pointer to that object, so it's probably something like 4 bytes per element.

正如Felix King所建议的那样,你可以通过使用委托来减少这个单个绑定到容器元素。然而,内存中的节省被增加的时间开销抵消 - 每当事件发生在容器中的任何位置时调用处理程序,并且必须测试目标是否与委托该事件的选择器匹配。

As Felix King suggested, you can reduce this by using delegation, as there's just a single binding to a container element. However, the savings in memory is offset by increased time overhead -- the handler is invoked every time the event occurs anywhere in the container, and it has to test whether the target matches the selector to which the event is delegated.

这篇关于Javascript绑定会占用内存而不使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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