-
Notifications
You must be signed in to change notification settings - Fork 199
/
expensive-keys.js
75 lines (64 loc) · 1.79 KB
/
expensive-keys.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
// measures how much memory a key and its value takes up in a collection of objects
(function expensiveKeysInit(root) {
function stringSize(str) {
// JavaScript strings are unicode UTF-16 up to 2 bytes per character
return str.length * 2;
}
function objectSize(obj) {
return stringSize(JSON.stringify(obj));
}
var value = function value(key) {
return function (object) {
return object[key];
};
};
var pickValue = function (key, items) {
return items.map(value(key));
};
function keySize(items, key) {
return stringSize(key) * items.length + objectSize(pickValue(key, items));
}
function zip(keys, values) {
var result = {};
keys.forEach(function (key, index) {
result[key] = values[index];
});
return result;
}
function toMB(bytes) {
return bytes / 1024 / 1024;
}
function toSizeMB(size) {
return toMB(size).toFixed(2) + ' MB';
}
function valuesInMB(obj) {
var result = {};
Object.keys(obj).forEach(function (key) {
var val = obj[key];
if (typeof val === 'number') {
result[key] = toSizeMB(obj[key]);
}
});
return result;
}
function propertySizes(keys, items) {
if (arguments.length === 1) {
items = keys;
if (!Array.isArray(items) && typeof items === 'object') {
items = [items];
}
if (!items.length) {
return {};
}
keys = Object.keys(items[0]);
}
var keyInItemsSize = keySize.bind(null, items);
var sizes = keys.map(keyInItemsSize);
var result = zip(keys, sizes);
result.mb = valuesInMB.bind(null, result);
return result;
}
root.expensiveKeys = propertySizes;
console.log('try expensiveKeys(<array of objects>);');
console.log('you can call .mb() method on the returned object');
}(this));