Skip to content

Instantly share code, notes, and snippets.

@rynbyjn
Created February 13, 2014 22:33
Show Gist options
  • Save rynbyjn/8985337 to your computer and use it in GitHub Desktop.
Save rynbyjn/8985337 to your computer and use it in GitHub Desktop.
Calculate rough size of an object in javascript (bytes)
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