-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizeofvar.js
116 lines (113 loc) · 2.31 KB
/
sizeofvar.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function sizeofvar(object, level, from_array) {
var type = false;
if (typeof level == "undefined") {
level = 0;
}
if (typeof from_array == "undefined") {
from_array = false;
}
var size = 0;
if (typeof object != "object") {
type = typeof object;
} else {
type = Object.prototype.toString.call(object);
if (type.includes('Array')) {
type = "array";
} else {
type = "object";
}
}
switch(type) {
case "boolean":
size += 152;
// size += 152;
break;
case "number":
var bit32 = 256 * 256 * 256 * 256 / 2;
var offset = 16;
if (level == 0) {
} else {
if (!from_array) {
offset = 32;
}
}
size += 152;
// size += (object>0?168:152);
size += (object>=bit32?offset:0);
// size += (Math.ceil(object / bit32) * 16);
if (from_array) {
size += 8;
}
break;
case "string":
size += 144;
// size += 152 + (object.length>0?24:0) + (Math.ceil(object.length / 8) * 8);
var offset = 24;
if (level == 0) {
size += 8;
}
size += (object.length>0?offset:0) + (Math.ceil(object.length / 8) * 8);
break;
case "object":
if (level == 0) {
size += 208;
} else {
size += 56;
}
// for (var key in objects[index]){
var c = 0;
Object.entries(object).forEach(([key, o]) => {
switch(typeof o) {
case "boolean":
size -= 152;
break;
case "number":
size -= 152;
break;
case "string":
size -= 144;
break;
}
size += sizeofvar(o,(level+1));
// console.log(key);
// console.log(typeof o);
// console.log(o);
c += 1;
});
if (c > 0) {
c -= 1;
if (c > 0) {
size += 40;
size += (Math.ceil(c / 3) * 24);
}
}
break;
case "array":
if (level == 0) {
size += 184;
} else {
size += 32;
}
size += (object.length>0?16:0);
object.forEach(function(o,index) {
switch(typeof o) {
case "boolean":
size -= 144;
// if (object.length > 0) {
// size -= 8;
// }
break;
case "number":
size -= 152;
break;
case "string":
size -= 136;
break;
}
size += sizeofvar(o,(level+1), true);
});
break;
}
return size;
}
module.exports = sizeofvar;