×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Added: Apr 9, 2015 7:21 PM
Views: 1845
Tags: bytesobject
  1. function roughSizeOfObject( object ) {
  2.  
  3.     var objectList = [];
  4.     var stack = [ object ];
  5.     var bytes = 0;
  6.  
  7.     while ( stack.length ) {
  8.         var value = stack.pop();
  9.  
  10.         if ( typeof value === 'boolean' ) {
  11.             bytes += 4;
  12.         }
  13.         else if ( typeof value === 'string' ) {
  14.             bytes += value.length * 2;
  15.         }
  16.         else if ( typeof value === 'number' ) {
  17.             bytes += 8;
  18.         }
  19.         else if
  20.         (
  21.             typeof value === 'object'
  22.             && objectList.indexOf( value ) === -1
  23.         )
  24.         {
  25.             objectList.push( value );
  26.  
  27.             for( var i in value ) {
  28.                 stack.push( value[ i ] );
  29.             }
  30.         }
  31.     }
  32.     return bytes;
  33. }