Created
February 13, 2014 22:33
-
-
Save rynbyjn/8985337 to your computer and use it in GitHub Desktop.
Calculate rough size of an object in javascript (bytes)
This file contains 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
roughSizeOfObject: (object) -> | |
objectList = [] | |
stack = [object] | |
bytes = 0 | |
while stack.length | |
value = stack.pop() | |
if typeof value is "boolean" | |
bytes += 4 | |
else if typeof value is "string" | |
bytes += value.length * 2 | |
else if typeof value is "number" | |
bytes += 8 | |
else if typeof value is "object" and objectList.indexOf(value) is -1 | |
objectList.push value | |
for i of value | |
stack.push value[i] | |
bytes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment