Javascript closures on heap or stack?

夙愿已清 提交于 2019-12-10 12:43:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!