showing results for - "javascript size of variable in kb"
Stefano
19 Jan 2019
1function roughSizeOfObject( object ) {
2
3    var objectList = [];
4
5    var recurse = function( value )
6    {
7        var bytes = 0;
8
9        if ( typeof value === 'boolean' ) {
10            bytes = 4;
11        }
12        else if ( typeof value === 'string' ) {
13            bytes = value.length * 2;
14        }
15        else if ( typeof value === 'number' ) {
16            bytes = 8;
17        }
18        else if
19        (
20            typeof value === 'object'
21            && objectList.indexOf( value ) === -1
22        )
23        {
24            objectList[ objectList.length ] = value;
25
26            for( i in value ) {
27                bytes+= 8; // an assumed existence overhead
28                bytes+= recurse( value[i] )
29            }
30        }
31
32        return bytes;
33    }
34
35    return recurse( object );
36}
37