Javascript闭包在堆或堆栈? [英] Javascript closures on heap or stack?

查看:200
本文介绍了Javascript闭包在堆或堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript(根据标准)在哪里存储closures:heap或stack?

闭包有第三个显式位置吗?

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

推荐答案

最后,它是运行时的实现细节。 请参阅Phoenix链接

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

对于实现,为了在上下文被销毁之后存储局部变量,基于栈的实现不再适合(因为它违背基于栈的结构的定义)。因此,在这种情况下,使用垃圾收集器(GC)和引用计数,将父上下文的闭包数据保存在动态存储器分配(在堆,即基于堆的实现)中。这种系统的速度不如基于堆栈的系统有效。然而,实现可能总是优化它:在解析阶段找出是否在函数中使用自由变量,并且根据这个决定 - 将数据放入堆栈或堆。

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".

这篇关于Javascript闭包在堆或堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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