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?

4
  • possible duplicate of Heap and Native memory allocation in JavaScript: how managed? Commented Sep 29, 2014 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 VM, which is stackless. Commented Sep 29, 2014 at 15:48
  • 2
    Have a look at Understanding stack and frame in javascript. Notice that your function could be trivially tail-call-optimized by a compiler. Commented Sep 29, 2014 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? Commented Feb 26, 2017 at 0:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.