Wednesday, July 3, 2024
HomeLanguagesJavascriptHow does memory stacks work in Javascript ?

How does memory stacks work in Javascript ?

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.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments