-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectSizeCalculator.js
77 lines (67 loc) · 2.07 KB
/
objectSizeCalculator.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
/**
* @todo: a function which calculates the size of an input object
* This function recurses through an object structure, finds all object within it, then
*/
function objectSizeCalculator(object) {
//analyse the content of the object to be sized
let content = objectContent(object);
let size = 0;
content.forEach((item) => {
size += sizeOfType(item);
});
return size;
}
/**
* @todo: takes an object, and travels it to detect all objects within, stores them in an array.
* In the end, it returns a basic array where all items are primitive types.
*/
function objectContent(object) {
let pile = [];
const content_ = Object.entries(object);
for(let [key, value] of content_) {
pile.push(key); // keys can only be 'string' typed in ECMA's specs
if(typeof(value) !== 'object') {
pile.push(value); // if value is a primitive type, just pile it
} else {
let innerPile = objectContent(value); // otherwise, depth-examine value
pile = pile.concat(innerPile); // and pile its decorticated structure
}
}
return pile;
}
/**
* @todo: returns the size of a primitive object based on its type
* Here are the types:
Boolean, Null, Undefined, Number, BigInt, String, Symbole.
*/
function sizeOfType(object) {
const type = typeof(object);
switch(type) {
case 'boolean':
return TypeSize.BOOLEAN;
case 'string':
return TypeSize.STRING;
case 'number':
return TypeSize.NUMBER;
case 'undefined':
return TypeSize.UNDEFINED;
case 'null':
return TypeSize.NULL;
}
}
// Size of primitive types in Bytes
let TypeSize = {
STRING: 2,
BOOLEAN: 4,
NUMBER: 8,
UNDEFINED: 0,
NULL: 0
}
TypeSize = Object.freeze(TypeSize);
//-----------------------------------------------------
// example of use
// let obj = {a: 1, b: {c: 2}, d: {e: {f: {g: 3}}}}
// let content = objectContent(obj);
// let size = objectSizeCalculator(obj);
// console.log(content);
// console.log(size);