Understanding Stack And Frame In Javascript
Solution 1:
So if I have a recursive js function, do all the recursive calls create a stack frame on the stack?
Yes. Every function call creates a new stack frame.
and then get popped off once we return?
Yes. There is currently no tail-call-elimination implemented in common engines, and it's not (yet) required by the spec.
Is the heap involved in this process?
Possibly. Or maybe not. The specification does not distinguish between stack and heap at all, and you cannot control it by means of the language anyway.
I am very confused with where things go. Does the first function get put on the heap?
Possibly. As said above, it's the engine's choice where things go; but you can expect that it's making educated decisions.
Functions are first-class objects in JavaScript, and I'd expect them to be put in the heap (notice that in a recursive call there typically is only one function anyway, they don't get duplicated). The variables in the stack frames on the stack would only contain primitive values and pointers to objects.
Post a Comment for "Understanding Stack And Frame In Javascript"