1

I'm trying to make a memory model for a simple piece of code I have written but I'm not sure how memory works in javascript. Here is the code:

//Recursion.js
var addMe = 0; //simple number to increase

//Recursively add 1 to addMe
var recursiveAddition = function (){
    addMe++;
    console.log(addMe);
    recursiveAddition(); //recursively restart timeout

   }

  recursiveAddition();

My guess is that following addMe, recursiveAddition just keeps piling up on the stack until I reach the max? No usage of heap in this case? Would this be a correct assumption? Is there other general particularities about javascript(contrary to c++) memory management that I should know about?

user2997154
  • 415
  • 7
  • 18
  • possible duplicate of [Heap and Native memory allocation in JavaScript: how managed?](http://stackoverflow.com/questions/20007263/heap-and-native-memory-allocation-in-javascript-how-managed) – Tetsujin no Oni Sep 29 '14 at 15:48
  • 5
    Concepts like "stack" or "heap" are usually associated with an implementation, not a language. For instance, Javascript can be hosted by the [Parrot](http://www.parrot.org/) VM, which is stackless. – Frédéric Hamidi Sep 29 '14 at 15:48
  • 2
    Have a look at [Understanding stack and frame in javascript](http://stackoverflow.com/q/25950026/1048572). Notice that your function could be trivially tail-call-optimized by a compiler. – Bergi Sep 29 '14 at 17:10
  • @FrédéricHamidi Would this be actually more true for a higher-level language though. In lower-level languages, such as Rust/C++, the idea of stack and heap seems to be quite an inherent part of the language itself, and it's harder to separate the implementation from the language, or at least harder/impractical to come up with a drastically different (potentially stackless) implementation. Is this understanding correct? – xji Feb 26 '17 at 00:06

0 Answers0