Skip to content

Instantly share code, notes, and snippets.

@bgadrian
Created March 16, 2015 17:16
Show Gist options
  • Save bgadrian/ac30b094ba2fbb5aa55e to your computer and use it in GitHub Desktop.
Save bgadrian/ac30b094ba2fbb5aa55e to your computer and use it in GitHub Desktop.
Script JS memory size object/array
/* cruel way to sum your global vars */
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 bytesToSize(bytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment