Open In App

How does memory stacks work in Javascript ?

Improve
Improve
Like Article
Like
Save
Share
Report

Introduction:

 The memory stack is a mechanism in JavaScript that allows us to allocate and free memory. 

If a programmer wants to store some data, they must first create an empty part of the heap that is called a “stack.” Then, they can push and then pop values on the stack. When working with strings in JavaScript, we typically use string concatenation as one way of pushing data onto the stack (without having to manage memory management manually). 

Note that code like this will not work with arrays and objects because those features do not use the word “stack” anywhere in their process (in order for them to be used effectively, one would have to understand how their internal structuring alters this concept).

Working of memory stack in Javascript: 

  • The memory stack is a data structure that allows us to allocate and free memory in JavaScript. 
  • It can be conceptualized as a stack of objects (which are essentially name/value pairs). 
  • When we create an object in JavaScript, it is placed on top of the memory stack. 
  • When we declare a variable using the var keyword, it gets its own link on the stack. 
  • When we use push() function, it sends a value to the stack and when we call pop() function, it retrieves an item from the top of the memory stack and removes it from there (if there is nothing at its top after iterating through all values with for loop, then its .length property gets set to 0).

Declaring a variable: Consider the following snippet.

var name = “James”; 
var age = 35;

The above variable declaration creates two new variables and stores their respective values. 

  • If you have a larger number of variables then they are stored in the same order as they are declared in the source code. 
  • Each one is placed on top of the memory stack, one successive layer at a time. 

This can be visualized as shown below:
When we declare a variable using the var keyword, its name gets associated with an empty part of the heap which is known as a variable object.

Applications of memory stack work in Javascript:

  • Concatenating strings: When dealing with data that contains strings, we often use concatenation as one way of pushing data onto the stack (without having to manage memory management manually). This can be done using the ” + ” operator. 
  • JavaScript execution engine does not actually follow a stack-based approach to function calls at runtime.

Important points: 

  • Overflow – main pile and overflow pile: When we use the push() function, it sends a value to the stack. 
  • It keeps appending values to the top of the heap until there isn’t enough space (i.e., whenever it encounters an empty part in the heap, it marks it as being in use). 
  • The value that is sent to this “overflow” then goes into another part of the memory heap which is called Off-Main-Pile (OMP). This second memory heap is also known as “overflow” or “spill” area. 
  • So you can see that two heaps were created for us by JavaScript. 
    The main heap contains variables that have been declared using the var statement, while the other heap contains all other variables. 

Conclusion: 

Memory stack in javascript is an important programming tool. Memory stack can be visualized as a stack of objects on which we can push and pop values. JavaScript execution engine does not actually follow a stack-based approach to function calls at runtime.


Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads