问题
Where does JavaScript (according to the standard) store closures: heap or stack?
Is there a third explicit place for closures?
回答1:
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”.
来源:https://stackoverflow.com/questions/16959342/javascript-closures-on-heap-or-stack