-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSizeOfObject.js
More file actions
53 lines (48 loc) · 1.22 KB
/
SizeOfObject.js
File metadata and controls
53 lines (48 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Size of object in bytes.
*
* @author [Andrej Hristoliubov]{@link https://github.com/anhr}
* Thanks to https://stackoverflow.com/a/11900218/5175935
*
* @copyright 2011 Data Arts Team, Google Creative Lab
*
* @license under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
/**
*
* @param {object} object The object whose size you want to know
* @returns size of object in bytes
*/
export default 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 ( var i in value ) {
stack.push( value[i] );
}
} else if ( ( typeof value !== "function" ) && ( value !== null ) )
console.error( 'typeof value = ' + typeof value );
}
return bytes;
}