Created
July 5, 2012 13:10
-
-
Save luis-almeida/3053574 to your computer and use it in GitHub Desktop.
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
// http://stackoverflow.com/questions/1248302/javascript-object-size | |
function roughSizeOfObject ( object ) { | |
var objectList = []; | |
var recurse = function ( value ) { | |
var bytes = 0; | |
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[ objectList.length ] = value; | |
for ( i in value ) { | |
bytes += 8; // an assumed existence overhead | |
bytes += recurse( value[i] ); | |
} | |
} | |
return bytes; | |
}; | |
return recurse( object ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment