Skip to content
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?
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md
index.js
memorySizeOf.js

README.md

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
You can’t perform that action at this time.