Created
August 4, 2013 02:02
-
-
Save jerrymarino/6148763 to your computer and use it in GitHub Desktop.
ECMAScript object size
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
function roughSizeOfObject( object ) { | |
var objectList = []; | |
var stack = [ object ]; | |
var bytes = 0; | |
while ( stack.length ) { | |
var value = stack.pop(); | |
if ( typeof value === 'boolean' ) { | |
bytes += 4; | |
} | |
else if ( typeof value === 'string' ) { | |
bytes += value.length * 2; | |
} | |
else if ( typeof value === 'number' ) { | |
bytes += 8; | |
} | |
else if | |
( | |
typeof value === 'object' | |
&& objectList.indexOf( value ) === -1 | |
) | |
{ | |
objectList.push( value ); | |
for( i in value ) { | |
stack.push( value[ i ] ); | |
} | |
} | |
} | |
return bytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment