-
Notifications
You must be signed in to change notification settings - Fork 3
/
bench.js
37 lines (35 loc) · 994 Bytes
/
bench.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
const {basename} = require('path')
const Benchmark = require('benchmark')
const Table = require('cli-table')
const glob = require('glob')
const getTable = name =>
new Table({
head: [name, 'mean', 'error'],
colWidths: [40, 10, 10]
})
const prettyNumber = value =>
Benchmark.formatNumber(value.toFixed(value < 100 ? 2 : 0))
const prettyMoe = value =>
`${value.toFixed(2)}%`
const pattern = '.bench.js'
glob(`**/*${pattern}`, (_, files) => {
for (let file of files) {
const tests = require(`./${file}`)
const name = basename(file, pattern)
const table = getTable(name)
const suite = new Benchmark.Suite(name, {
onComplete: () => { console.log(table.toString()) }
})
Object.keys(tests).forEach(key =>
suite.add(key, tests[key], {
onComplete: ({target: {name, stats}}) =>
table.push([
name,
prettyNumber(stats.mean),
prettyMoe(stats.rme)
])
})
)
suite.run()
}
})