Skip to content

Instantly share code, notes, and snippets.

@thysultan
Last active June 13, 2016 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thysultan/743753c9efa99f727f6c3ca8a2cc1c08 to your computer and use it in GitHub Desktop.
Save thysultan/743753c9efa99f727f6c3ca8a2cc1c08 to your computer and use it in GitHub Desktop.
calculate memory size of javascript object, it is not a accurate value!
function memory(obj) {
var bytes = 0;
function size (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':
if (obj[_c] === Object || obj[_c] === Array) {
for (var key in obj) {
if (!obj.hasOwnProperty(key)) {
continue
}
size(obj[key])
}
} else {
bytes += obj.toString().length * 2
}
break
}
}
return bytes
}
return size(obj) / 1024
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment