How does memory stacks work in Javascript ?
Last Updated :
06 Apr, 2023
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.
Similar Reads
How to Handle Memory Leaks in JavaScript ? JavaScript, unlike some other programming languages, uses automatic memory management. This means the developer doesn't have to explicitly free up memory when they're done with it. However, memory leaks can still happen in JavaScript if objects are not properly cleaned up. A memory leak is a situati
9 min read
How closure works in JavaScript ? In this article, we will discuss about the closures working JavaScript. Let us first understand what exactly closures are and basic details which are associated with closures in JavaScript.A Closure is a combination of a function enclosed with references to its surrounding state (the lexical environ
2 min read
How are strings stored in JavaScript ? In this article, we will try to understand how strings are stored in JavaScript. Strings are a primitive data type in JavaScript and are allocated a special place in memory to store and manipulate. Unlike some other languages, JavaScript does not have a String Constant Pool. Instead, JavaScript engi
3 min read
How does JavaScript Hoisting work internally ? In simple terms, hoisting in JavaScript refers to the fact that variables can be used before they're declared. The declared variables are somehow virtually moved to the top of the scope. However, hoisting applies only to variable declarations and not to variable initialization. While almost anyone
4 min read
How to Fix "JavaScript Heap Out of Memory" Error? We might have encountered an error that says "JavaScript heap out of memory" while working with JavaScript. This error occurs when our application uses more memory than the default limit set by Node.js. When this error occurs our program crashes and we cannot proceed with our work. In this article,
3 min read