Skip to content

Latest commit

 

History

History
36 lines (33 loc) · 955 Bytes

File metadata and controls

36 lines (33 loc) · 955 Bytes

Check how much memory an object occupies in JavaScript

JavaScript does not provide a sizeof (like in C), and programmer does not need to care about memory allocation/deallocation. But how can we check how much memory an object occupies?

Primitive types

Let's say that you have a string:

var stringObject =  String("Ala Eddine JEBALI");

How to implement a memorySizeOf function which returns the memory size of stringObject?

memorySizeOf(stringObject)

Other objects

Let's say that you have this object:

var personObject =  {persone: {name: 'Ala Eddine JEBALI', country: 'France', age: 30}};

How to implement a memorySizeOf function which returns the memory size of personObject?

memorySizeOf(personObject)

Remark

The result is of course the same when you want to check

console.log( memorySizeOf(123) );

or

console.log( memorySizeOf(Number(123)) );

The output is:

8 bytes