-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsuites.js
More file actions
47 lines (39 loc) · 1.5 KB
/
suites.js
File metadata and controls
47 lines (39 loc) · 1.5 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
const Benchmark = require('benchmark');
const report = require('beautify-benchmark');
const _ = require('lodash');
const numeral = require('numeral');
const fs = require('fs');
const exampleToml = fs.readFileSync('./data/example.toml', 'utf8');
const subjects = {
'js': require('./subjects/js-example'),
'rust-ffi': require('./subjects/neon-example/lib'),
'rust-wasm': require('./subjects/wasm-pack-example/pkg'),
'ts-wasm': require('./subjects/assemblyscript-example'),
};
const benchedFunctions = {
'sum': s => s.sum(1, 2),
'sha1': s => s.sha1('pls-sha1-me'),
'fibonacci': s => s.fibonacci(10000),
'parse TOML': s => s.readFieldFromToml(exampleToml, 'title'),
};
for (const functionName of Object.keys(benchedFunctions)) {
console.log(`== ${functionName} ==\n`);
const suite = new Benchmark.Suite;
for (const subjectName of Object.keys(subjects)) {
suite.add(subjectName, () => benchedFunctions[functionName](subjects[subjectName]))
}
suite.on('cycle', event => report.add(event.target));
suite.on('complete', () => {
logComparison(report);
report.log();
});
suite.run();
}
function logComparison(report) {
const benches = _.sortBy(report.store.filter(bench => bench.hz), bench => -bench.hz);
console.log(` ${benches[0].name} was the fastest`);
for (let i = 1; i < benches.length; i++) {
const performanceRatio = benches[0].hz / benches[i].hz;
console.log(` ${benches[i].name} was ${numeral(performanceRatio).format('0a')}x slower than ${benches[0].name}`)
}
}