Understanding Memory Allocation of Object in JavaScript

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

  1. 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?

  1. Heap Memory: When an object creates , JavaScript allocates memory dynamically in the Heap.
  2. 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 .



Mridul Goswami

Student at SUNDERDEEP ENGINEERING COLLEGE, DASNA, GHAZIABAD.

3 周

??

Rounak Yadav

Attended Sdgi global University

3 周

Bro keep it up ????????

要查看或添加评论,请登录

Ansh Shukla的更多文章

  • Understanding the Working of the JavaScript

    Understanding the Working of the JavaScript

    Ever wondered how JavaScript code actually runs behind the scenes? Understanding the JavaScript execution process is…

    2 条评论

社区洞察

其他会员也浏览了