Search code examples
javascriptoptimizationheap-memorygeneratorv8

Are generator function's local variable stored on Stack or Heap?


In this article: https://wingolog.org/archives/2013/06/11/ecmascript-generators-from-a-performance-perspective, it mentioned that:

In a generator function, V8 stores local variables on the heap instead of on the stack.

but it also contradicts itself with this next passage:

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,

The part that I don't understand is that generator functions always use yield, and I would assume that the suspension mentioned in the article refers to yield statement.


Solution

  • I think the article is missing an example:

      return 12 + yield 5
    

    In this case, 12 has to be allocated on the stack, and when suspending the iterator, it has to be copied off the stack, and then onto the stack again when the iterator continues. The second paragraph talks about "temporaries" (12 in this case), not about "variables".