Last active
June 13, 2016 19:43
-
-
Save thysultan/743753c9efa99f727f6c3ca8a2cc1c08 to your computer and use it in GitHub Desktop.
calculate memory size of javascript object, it is not a accurate value!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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