Skip to content

Instantly share code, notes, and snippets.

@luis-almeida
Created July 5, 2012 13:10
Show Gist options
  • Save luis-almeida/3053574 to your computer and use it in GitHub Desktop.
Save luis-almeida/3053574 to your computer and use it in GitHub Desktop.
// 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