Understanding Memory Allocation of Object in JavaScript
Introduction
JavaScript is a widely used language for making websites. it's essential to know how JavaScript handle the memory allocation of an object.
Before diving into memory allocation, let's first understand the different ways to create an object in JavaScript.
Two main ways to create an object
- Literals:
it is the simplest way of creating an object. we can define an object using curly braces.
for example:
Characteristics :
- Best for static object.
- No need for constructor functions.
- Directly creates an object in memory.
2. new Keyword:
- it is another way of creating or define an object.
- it is constructor based creation of an object.
- Create an object by using a new built in keyword with constructor function.
For example:
characteristics:
- Used to create object dynamically.
- Not recommend for creation of regular object (static object).
- Calls the object() constructor adding extra processing overhead.
Memory Allocation of object
JavaScript manages memory using Heap memory and Stack memory.
How Memory is Allocated?
- Heap Memory: When an object creates , JavaScript allocates memory dynamically in the Heap.
- Stack Memory: The reference of the object is stored in the stack memory.
for example:
Here, let person stores in the Stack Memory and {name:"xyz",age:25}; stores in the heap memory .
Student at SUNDERDEEP ENGINEERING COLLEGE, DASNA, GHAZIABAD.
3 周??
Java || MERN
3 周Informative
Attended Sdgi global University
3 周Bro keep it up ????????