0

I am referencing a answer from stack overflow -->> V8 uses a heap similar to JVM and most other languages. This, however, means that local variables (as a general rule) are put on the stack and objects in the heap. This may for instance not hold if a function closes over these values. As in the JVM, primitives can only be stored on the stack if they are stored in a local variable.

As a user it is not something you would normally need to worry about.

  • Possible duplicate of [Where does a JavaScript closure live?](https://stackoverflow.com/q/37491626/1048572) and related questions – Bergi May 27 '20 at 20:30

2 Answers2

2

(V8 developer here.) You are correct, local variables in one function that another function closes over can't easily be stored on the stack. Specifically, in V8 they are stored in a so-called "Context" object on the heap, which is what @JonasWilms is referring to. To illustrate with an example:

function outer() {
  let a = 1;  // Will be on the stack
  let b = 2;  // Will be in the context on the heap
  return function inner() {
    return b;
  }
}

You are also absolutely correct that this is not something you need to worry about :-) In particular, because "stack" and "heap" really are internal implementation details; they just so happen to be the most common way how engines (for many languages) achieve the required behavior with decent performance.

More conceptually speaking, we're talking about scopes and lifetimes of variables here: in the example, a goes out of scope (=becomes unreachable) when outer finishes running, whereas b is still reachable via inner, so it lives on as long as inner could be called again. How an engine accomplishes that is an internal detail and could change at any time. For example, an engine could decide to simplify its implementation by always putting all variables on the heap, avoiding the need to analyze in advance which variables will be closed over and which won't. All that matters is the observable behavior, not how it's achieved under the hood.

jmrk
  • 34,271
  • 7
  • 59
  • 74
0

If you look at the v8 doc, you can see that Functions do have a Local<Context> attribute. Local is a wrapper class for values managed by the garbagge collector, so yes, the Context resides on the heap.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151