-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
134 lines (117 loc) · 3.69 KB
/
helpers.js
File metadata and controls
134 lines (117 loc) · 3.69 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const fastStableStringify = require('fast-json-stable-stringify')
function flattenJson(data, path = []) {
if (Array.isArray(data)) {
return data.reduce((acc, value, index) => {
const valueJson = flattenJson(value, [...path, index]);
for (const [key, value] of Object.entries(valueJson)) {
acc[key] = value;
}
return acc;
}, {});
} else if (typeof data === "object" && data !== null) {
return Object.keys(data).reduce((acc, key) => {
const keyJson = flattenJson(data[key], [...path, key]);
for (const [key, value] of Object.entries(keyJson)) {
acc[key] = value;
}
return acc;
}, {});
} else {
return { [path.join(".")]: data };
}
}
// https://stackoverflow.com/questions/1248302/how-to-get-the-size-of-a-javascript-object
function sizeOfObject(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]);
}
}
}
return bytes;
}
function base64Decode(data) {
return data && Buffer.from(data, "base64").toString();
}
function base64Encode(data) {
return data && Buffer.from(data).toString("base64");
}
function percentile(arr, p) {
if (typeof p !== 'number' || p <= 0 || p >= 1.0) {
throw new Error('Percentile must be a number between 0 and 1');
}
const sorted = [...arr].sort((a, b) => a - b);
const index = p * (sorted.length - 1);
const lower = Math.floor(index);
const upper = Math.ceil(index);
const weight = index - lower;
return sorted[lower] * (1 - weight) + sorted[upper] * weight;
}
function p50(values) {
return percentile(values, 0.5);
}
function p90(values) {
return percentile(values, 0.9);
}
function p99(values) {
return percentile(values, 0.99);
}
function stdDev(values, avg) {
const sum = values.reduce((acc, v) => acc + (v - avg) ** 2, 0);
return Math.sqrt(sum / values.length);
}
function stats(values) {
const sum = values.reduce((acc, v) => acc + v, 0);
const avg = sum / values.length;
return {
count: values.length,
min: Math.min(...values),
max: Math.max(...values),
stdDev: stdDev(values, avg),
sum,
avg,
p1: percentile(values, 0.01),
p5: percentile(values, 0.05),
p10: percentile(values, 0.1),
p20: percentile(values, 0.2),
p30: percentile(values, 0.3),
p40: percentile(values, 0.4),
p50: percentile(values, 0.5),
p60: percentile(values, 0.6),
p70: percentile(values, 0.7),
p80: percentile(values, 0.8),
p90: percentile(values, 0.9),
p95: percentile(values, 0.95),
p99: percentile(values, 0.99),
p999: percentile(values, 0.999),
};
}
function jsonStringify(data, stringifier = "stable") {
if (stringifier === "stable") {
return fastStableStringify(data)
} else if (stringifier === "default") {
return JSON.stringify(data)
} else {
throw new Error(`Invalid stringifier: ${stringifier}`)
}
}
module.exports = {
flattenJson,
sizeOfObject,
base64Decode,
base64Encode,
stats,
jsonStringify,
}