Skip to content
Advertisement

Javascript closures on heap or stack?

Where does JavaScript (according to the standard) store closures: heap or stack?
Is there a third explicit place for closures?

Advertisement

Answer

In the end it is an implementation detail of the runtime. See Phoenix link

As to implementations, for storing local variables after the context is destroyed, the stack-based implementation is not fit any more (because it contradicts the definition of stack-based structure). Therefore in this case closured data of the parent context are saved in the dynamic memory allocation (in the “heap”, i.e. heap-based implementations), with using a garbage collector (GC) and references counting. Such systems are less effective by speed than stack-based systems. However, implementations may always optimize it: at parsing stage to find out, whether free variables are used in function, and depending on this decide — to place the data in the stack or in the “heap”.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement