-
Notifications
You must be signed in to change notification settings - Fork 12
/
benchmark.js
40 lines (30 loc) · 1.43 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
const Benchmark = require('benchmark');
const config = require('./config');
const { minSamples, minimumFractionDigits, misteryMessage: MISTERY_MESSAGE } = config;
const suite = new Benchmark.Suite;
const functions = require('require-all')({
dirname: `${__dirname}/functions`,
});
const compare = (a, b) => b.hz - a.hz;
const onError = err => console.error(err.message);
const addAll = s => Object.entries(functions).reduce((agg, [k, v]) => agg.add(k, () => v(MISTERY_MESSAGE), { onError, minSamples }), s);
const secsToMicrosecs = seconds => seconds * 1000000;
const printStandingArray = ({ name, hz, stats }, index, standingsArray) => ({
name,
mean: `${secsToMicrosecs(stats.mean).toLocaleString('en-US', { minimumFractionDigits }) } μs`,
diffWithFastestMean: `${(stats.mean / standingsArray[0].stats.mean).toLocaleString('en-US', { minimumFractionDigits })}x`,
opsPerSecond: hz.toLocaleString('en-US', { minimumFractionDigits }),
diffWithFastest: `${(standingsArray[0].hz / hz).toLocaleString('en-US', { minimumFractionDigits })}x`,
result: functions[name](MISTERY_MESSAGE),
})
console.log('Running benchmark:');
addAll(suite)
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('\nFastest is ' + this.filter('fastest').map('name').join(', '));
console.table(this.sort(compare).map(printStandingArray));
})
.on('error', onError)
.run({ 'async': true });