在这篇文章中: https://wingolog.org/archives/2013/06/11/ecmascript-generators-from-a-performance-perspective ,它提到:
In a generator function, V8 stores local variables on the heap instead of on the stack.
但它也与下一段内容相矛盾:
The exception to this case is when you yield and there are temporaries on the stack. Recall in my article on V8's baseline compiler that the full-codegen is a stack machine. It allocates slots to named locals, but temporary values go on the stack at run-time,
我不明白的部分是生成器函数总是使用yield,我假设文章中提到的暂停指的是yield语句。
最佳答案
我认为这篇文章缺少一个示例:
return 12 + yield 5
在这种情况下,必须在堆栈上分配 12,并且当挂起迭代器时,必须将其从堆栈中复制出来,然后在迭代器继续时再次复制到堆栈上。第二段讨论“临时变量”(本例中为 12 个),而不是“变量”。
关于javascript - 生成器函数的局部变量存储在堆栈还是堆上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55732633/