-
Notifications
You must be signed in to change notification settings - Fork 8
/
benchmark.js
50 lines (42 loc) · 1.16 KB
/
benchmark.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
const Table = require('cli-table');
const timings = {};
const sizes = {};
const iterations = 50;
const count = 1500;
const ms = time => `${time}ms`
const perform = (target, fn) => {
// Warn the cache
for (var i = 0; i < 5; i++) {
fn();
}
for (var i = 0; i < iterations; i++) {
const start = Date.now();
const size = fn().length;
const time = Date.now() - start;
timings[target]
? timings[target].push(time)
: timings[target] = [time];
sizes[target] = size;
}
}
const table = new Table({
head: ['Library', 'Average', 'Median', 'Min', 'Max', 'Output Size'],
});
const report = (target, log) => {
const times = timings[target].sort((a, b) => {
if (a === b) return 0;
return a > b ? 1 : -1;
});
const sum = times.reduce((a, b) => a + b);
const average = sum / times.length;
const end = times.length - 1;
const mid = Math.round(times.length / 2);
table.push(
[target, ms(average), ms(times[mid]), ms(times[0]), ms(times[end]), sizes[target]]
);
if (log) {
console.log(`Rendering ${count} components ${iterations} times.`);
console.log(table.toString());
}
}
module.exports = { perform, report, count }