Skip to content

Instantly share code, notes, and snippets.

Embed
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;
break;
case 'string':
bytes += obj.length * 2;
break;
case 'boolean':
bytes += 4;
break;
case 'object':
var objClass = Object.prototype.toString.call(obj).slice(8, -1);
if(objClass === 'Object' || objClass === 'Array') {
for(var key in obj) {
if(!obj.hasOwnProperty(key)) continue;
sizeOf(obj[key]);
}
} else bytes += obj.toString().length * 2;
break;
}
}
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));
};
@globalblob

This comment has been minimized.

Copy link

commented Sep 28, 2016

Thanks for sharing!

@Jevin23

This comment has been minimized.

Copy link

commented Oct 17, 2016

length of name key in object not calculating in this code

@wahengchang

This comment has been minimized.

Copy link

commented May 4, 2017

it s awesome !!! thx !

@ArhamAliQureshi

This comment has been minimized.

Copy link

commented Nov 25, 2017

Thanks a lot....!

@kntrieu

This comment has been minimized.

Copy link

commented Jan 10, 2018

Thanks for sharing! :-)

@clewrus

This comment has been minimized.

Copy link

commented Jun 1, 2018

Thanks !!!

@lucdeit1997

This comment has been minimized.

Copy link

commented Jun 9, 2018

NO AWESOME,NO AMAZING, AND NO..., I DON'T NEED, IT'S ..

@StefansArya

This comment has been minimized.

Copy link

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.

@ganeshmogare

This comment has been minimized.

Copy link

commented Sep 18, 2018

Super cool !!!! thanks buddy !!!

@hayk-manasyan

This comment has been minimized.

Copy link

commented Nov 15, 2018

Supper! Thanks

@aadityataparia

This comment has been minimized.

Copy link

commented Jan 17, 2019

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

@afwn90cj93201nixr2e1re

This comment has been minimized.

Copy link

commented Feb 7, 2019

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

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

try to get size of this object

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

@miadian

This comment has been minimized.

Copy link

commented Feb 14, 2019

it should be:

sizeOf(key);
if(!obj.hasOwnProperty(key)) continue;
sizeOf(obj[key]); 

on lines 20,21.

@gi-joe-moto

This comment has been minimized.

Copy link

commented Feb 19, 2019

or

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

This comment has been minimized.

Copy link

commented May 8, 2019

this is awesome! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.