Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
calculate memory size of javascript object, it is not a accurate value!
function memorySizeOf(obj) {
var bytes = 0;
function sizeOf(obj) {
if(obj !== null && obj !== undefined) {
switch(typeof obj) {
case 'number':
bytes += 8;
case 'string':
bytes += obj.length * 2;
case 'boolean':
bytes += 4;
case 'object':
var objClass =, -1);
if(objClass === 'Object' || objClass === 'Array') {
for(var key in obj) {
if(!obj.hasOwnProperty(key)) continue;
} else bytes += obj.toString().length * 2;
return bytes;
function formatByteSize(bytes) {
if(bytes < 1024) return bytes + " bytes";
else if(bytes < 1048576) return(bytes / 1024).toFixed(3) + " KiB";
else if(bytes < 1073741824) return(bytes / 1048576).toFixed(3) + " MiB";
else return(bytes / 1073741824).toFixed(3) + " GiB";
return formatByteSize(sizeOf(obj));
Copy link

globalblob commented Sep 28, 2016

Thanks for sharing!

Copy link

Jevin23 commented Oct 17, 2016

length of name key in object not calculating in this code

Copy link

wahengchang commented May 4, 2017

it s awesome !!! thx !

Copy link

ArhamAliQureshi commented Nov 25, 2017

Thanks a lot....!

Copy link

kntrieu commented Jan 10, 2018

Thanks for sharing! :-)

Copy link

clewrus commented Jun 1, 2018

Thanks !!!

Copy link

lucdeit1997 commented Jun 9, 2018


Copy link

StefansArya commented Jun 13, 2018

I think it's better for not calculate keys that refer to an object, as it can be a circular object or that keys was only referring to another object value.

Copy link

ganeshmogare commented Sep 18, 2018

Super cool !!!! thanks buddy !!!

Copy link

hayk-manasyan commented Nov 15, 2018

Supper! Thanks

Copy link

aadityataparia commented Jan 17, 2019

it should be bytes += sizeOf(obj[key]) + sizeOf(key); on line 21

Copy link

afwn90cj93201nixr2e1re commented Feb 7, 2019

it should be bytes += sizeOf(obj[key]) + sizeOf(key); on line 21

{ "login":"аааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааа", "password":"аааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааааа" }

try to get size of this object

with your edit size = ~1.2kb
but real size is ~300-400bytes

Copy link

miadian commented Feb 14, 2019

it should be:

if(!obj.hasOwnProperty(key)) continue;

on lines 20,21.

Copy link

gi-joe-moto commented Feb 19, 2019


if (objClass != 'Array'){
  bytes += 2 * key.length;

Copy link

hunted-down-developer commented May 8, 2019

this is awesome! thanks!

Copy link

akleandrov commented Sep 10, 2019

why boolean is 4 bytes? Maybe you have spec link?

Copy link

shevchenkonik commented Nov 26, 2019

why boolean is 4 bytes? Maybe you have spec link?


A boolean is actually 1 byte. But alignment may cause 4 bytes to be used on a 32-bit platform (or 8 bytes on a 64-bit platform). This is an old trick that comes from the observation that allocated memory takes up at least 4 or 8 bytes, and are aligned in such a way that the least significant bit or three will be zero.

Javascript engines (for example V8) are implemented using C++. You can find documentation about the size of boolean values in C++ spec. [1]

More information:
[1] ––

Copy link

shevchenkonik commented Nov 28, 2019

The function sizeOf doesn't work with Uint8Array and other standard types of arrays.

Copy link

sankethmandapati commented Jan 9, 2020

Thankyou, it works

Copy link

begoat commented Mar 26, 2020


Copy link

sagar-gavhane commented Jun 14, 2020

If someone looking for npm library to calculate size of an object in memory then give a try to this library:

Copy link

liSong5713 commented Jun 28, 2020

   `         bytes += obj.length * 2;`  why  obj.length*2 ,it should be Buffer.byteLength(str)

Copy link

ImCityHunter commented Feb 15, 2021

why bytes += obj.toString().length * 2;

Copy link

0xhex commented May 19, 2021

i am getting error :
node:1863) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
at Buffer.toString (buffer.js:776:46)

Copy link

kriit24 commented Nov 12, 2021

Im getting same error
Uncaught RangeError: Maximum call stack size exceeded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment