Does JavaScript use stack or heap for memory allocation or both?

View original thread
Satya's photo

To be short, V8 uses a heap similar to JVM, local variables are put on the stack and objects in the heap. Consider this code,

function foo(b) { var a = 10; return a + b + 11; }

function bar(x) { var y = 3; return foo(x * y); }


When we call bar() it will create a frame in stack with name bar and with respective arguments and when it ends it gets to pop out from stack, There really was no recursion in my code, if it has "Maximum call stack size exceeded" exception will be thrown, so stack frame is used in this way of memory allocation. I think most of the global variables use heap and it appears when memory leaks occur