4 ms·
This is silly. You're creating a new anonymous function on every interval, so it will increasingly take up memory to hold these anon functions until the GC kic
by balloot 14y ago
This is silly. You're creating a new anonymous function on every interval, so it will increasingly take up memory to hold these anon functions until the GC kicks in. It's doing exactly what it should do, because you don't want it doing GC every 50 ms.
- ajanuary 14y agoIt isn't creating a new anonymous function every interval. It's created once and passed in as an argument. It's presumably the call stack that the memory is used for.
- teraflop 14y agoI don't think that's what's happening. The anonymous function is created just once, before calling setInterval, not within the callback itself.
- abstractbill 14y agoThat anonymous function is a literal, just like the number 50 (the next argument after it). In the same way the 50 doesn't get created each iteration, neither does the anonymous function.
- balloot 14y agoThe memory profile says otherwise, doesn't it? If you create a named function and call it like "setInterval(draw, 50)", the memory use stays constant.
- ricardobeat 14y agoDid you get that result on your machine? From all reports in that question the profile is the same regardless of the anonymous function.
- ajanuary 14y agoExcept it doesn't. At least, not when I try it.
- enjo 14y agoMy tenuous understanding of the ECMAScript spec would suggest that a new execution context and associated activation object + scope chain is created each time that function is invoked. Since this is a closure, the execution context and associated objects have to wait to be garbage collected. So you're going to see an increase in memory with each function call until the garbage collector kicks in. So while the function definition is a literal, the execution most definitely has a cost associated with it.