Skip to content

Commit 543dfb1

Browse files
committed
chore: refactor the benchmarks to use tinybench
1 parent 7f02ba2 commit 543dfb1

File tree

15 files changed

+159
-255
lines changed

15 files changed

+159
-255
lines changed

package-lock.json

Lines changed: 0 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"@typescript-eslint/parser": "7.16.1",
4949
"assert": "2.1.0",
5050
"babel-loader": "9.2.1",
51-
"benchmark": "2.1.4",
5251
"c8": "10.1.2",
5352
"codecov": "3.8.3",
5453
"core-js": "3.39.0",
@@ -86,7 +85,6 @@
8685
"ndarray-ops": "1.2.2",
8786
"ndarray-pack": "1.2.1",
8887
"numericjs": "1.2.6",
89-
"pad-right": "0.2.2",
9088
"prettier": "3.3.3",
9189
"process": "0.11.10",
9290
"sinon": "19.0.2",

test/benchmark/algebra.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// test performance of the expression parser in node.js
22

3-
import Benchmark from 'benchmark'
4-
import padRight from 'pad-right'
5-
import { simplify, derivative } from '../../lib/esm/index.js'
6-
7-
function pad (text) {
8-
return padRight(text, 40, ' ')
9-
}
3+
import { Bench } from 'tinybench'
4+
import { derivative, simplify } from '../../lib/esm/index.js'
5+
import { formatTaskResult } from './utils/formatTaskResult.js'
106

117
const simplifyExpr = '2 * 1 * x ^ (2 - 1)'
128
const derivativeExpr = '2x^2 + log(3x) + 2x + 3'
@@ -18,19 +14,15 @@ console.log(' ' + derivative(derivativeExpr, 'x'))
1814

1915
const results = []
2016

21-
const suite = new Benchmark.Suite()
22-
suite
23-
.add(pad('algebra simplify '), function () {
17+
const bench = new Bench({ time: 100, iterations: 100 })
18+
.add('algebra simplify ', function () {
2419
const res = simplify(simplifyExpr)
2520
results.push(res)
2621
})
27-
.add(pad('algebra derivative'), function () {
22+
.add('algebra derivative', function () {
2823
const res = derivative(derivativeExpr, 'x')
2924
results.push(res)
3025
})
31-
.on('cycle', function (event) {
32-
console.log(String(event.target))
33-
})
34-
.on('complete', function () {
35-
})
36-
.run()
26+
27+
bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
28+
await bench.run()

test/benchmark/derivative.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// test performance of derivative
22

3-
import Benchmark from 'benchmark'
3+
import { Bench } from 'tinybench'
44
import { derivative, parse } from '../../lib/esm/index.js'
5+
import { formatTaskResult } from './utils/formatTaskResult.js'
56

67
let expr = parse('0')
78
for (let i = 1; i <= 5; i++) {
@@ -12,10 +13,7 @@ for (let i = 1; i <= 5; i++) {
1213

1314
const results = []
1415

15-
Benchmark.options.minSamples = 100
16-
17-
const suite = new Benchmark.Suite()
18-
suite
16+
const bench = new Bench({ time: 100, iterations: 100 })
1917
.add('ddf', function () {
2018
const res = derivative(derivative(expr, parse('x'), { simplify: false }), parse('x'), { simplify: false })
2119
results.splice(0, 1, res)
@@ -24,9 +22,6 @@ suite
2422
const res = derivative(expr, parse('x'), { simplify: false })
2523
results.splice(0, 1, res)
2624
})
27-
.on('cycle', function (event) {
28-
console.log(String(event.target))
29-
})
30-
.on('complete', function () {
31-
})
32-
.run()
25+
26+
bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
27+
await bench.run()

test/benchmark/expression_parser.js

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@
33
// browserify benchmark/expression_parser.js -o ./benchmark_expression_parser.js
44

55
import assert from 'node:assert'
6-
import Benchmark from 'benchmark'
7-
import padRight from 'pad-right'
8-
import { create, all } from '../../lib/esm/index.js'
6+
import { Bench } from 'tinybench'
7+
import { all, create } from '../../lib/esm/index.js'
98
import { getSafeProperty } from '../../lib/esm/utils/customs.js'
9+
import { formatTaskResult } from './utils/formatTaskResult.js'
1010

1111
const math = create(all)
1212

13-
// expose on window when using bundled in a browser
14-
if (typeof window !== 'undefined') {
15-
window.Benchmark = Benchmark
16-
}
17-
18-
function pad (text) {
19-
return padRight(text, 40, ' ')
20-
}
21-
2213
const expr = '2 + 3 * sin(pi / 4) - 4x'
2314
const scope = new Map([
2415
['x', 2]
@@ -49,40 +40,34 @@ assertApproxEqual(compiledPlainJs.evaluate(scope), correctResult, 1e-7)
4940
let total = 0
5041
const nodes = []
5142

52-
const suite = new Benchmark.Suite()
53-
suite
54-
.add(pad('(plain js) evaluate'), function () {
43+
const bench = new Bench({ time: 100, iterations: 100 })
44+
.add('(plain js) evaluate', function () {
5545
total += compiledPlainJs.evaluate(scope)
5646
})
5747

58-
.add(pad('(mathjs) evaluate'), function () {
48+
.add('(mathjs) evaluate', function () {
5949
total += compiled.evaluate(scope)
6050
})
61-
.add(pad('(mathjs) parse, compile, evaluate'), function () {
51+
.add('(mathjs) parse, compile, evaluate', function () {
6252
total += math.parse(expr).compile().evaluate(scope)
6353
})
64-
.add(pad('(mathjs) parse, compile'), function () {
54+
.add('(mathjs) parse, compile', function () {
6555
const node = math.parse(expr).compile()
6656
nodes.push(node)
6757
})
68-
.add(pad('(mathjs) parse'), function () {
58+
.add('(mathjs) parse', function () {
6959
const node = math.parse(expr)
7060
nodes.push(node)
7161
})
7262

73-
.on('cycle', function (event) {
74-
console.log(String(event.target))
75-
})
76-
.on('complete', function () {
77-
// we count at total to prevent the browsers from not executing
78-
// the benchmarks ("dead code") when the results would not be used.
79-
if (total > 1e6) {
80-
console.log('')
81-
} else {
82-
console.log('')
83-
}
84-
})
85-
.run()
63+
bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
64+
await bench.run()
65+
66+
// we count at total to prevent the browsers from not executing
67+
// the benchmarks ("dead code") when the results would not be used.
68+
if (total > 1e6) {
69+
console.log('')
70+
}
8671

8772
function assertApproxEqual (actual, expected, tolerance) {
8873
const diff = Math.abs(expected - actual)

test/benchmark/factorial.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import Benchmark from 'benchmark'
2-
import padRight from 'pad-right'
31
import BigNumber from 'decimal.js'
4-
5-
function pad (text) {
6-
return padRight(text, 40, ' ')
7-
}
2+
import { Bench } from 'tinybench'
3+
import { formatTaskResult } from './utils/formatTaskResult.js'
84

95
const results = []
106

@@ -45,55 +41,51 @@ function betterFactorial (n) {
4541
return prod
4642
}
4743

48-
const suite = new Benchmark.Suite()
49-
suite
50-
.add(pad('bigFactorial for small numbers'), function () {
44+
const bench = new Bench({ time: 100, iterations: 100 })
45+
.add('bigFactorial for small numbers', function () {
5146
const res = bigFactorial(new BigNumber(8))
5247
results.push(res)
5348
})
54-
.add(pad('new bigFactorial for small numbers'), function () {
49+
.add('new bigFactorial for small numbers', function () {
5550
const res = betterFactorial(new BigNumber(8))
5651
results.push(res)
5752
})
5853

59-
.add(pad('bigFactorial for small numbers 2'), function () {
54+
.add('bigFactorial for small numbers 2', function () {
6055
const res = bigFactorial(new BigNumber(20))
6156
results.push(res)
6257
})
63-
.add(pad('new bigFactorial for small numbers 2'), function () {
58+
.add('new bigFactorial for small numbers 2', function () {
6459
const res = betterFactorial(new BigNumber(20))
6560
results.push(res)
6661
})
6762

68-
.add(pad('bigFactorial for big numbers'), function () {
63+
.add('bigFactorial for big numbers', function () {
6964
const res = bigFactorial(new BigNumber(600))
7065
results.push(res)
7166
})
72-
.add(pad('new bigFactorial for big numbers'), function () {
67+
.add('new bigFactorial for big numbers', function () {
7368
const res = betterFactorial(new BigNumber(600))
7469
results.push(res)
7570
})
7671

77-
.add(pad('bigFactorial for HUGE numbers'), function () {
72+
.add('bigFactorial for HUGE numbers', function () {
7873
const res = bigFactorial(new BigNumber(1500))
7974
results.push(res)
8075
})
81-
.add(pad('new bigFactorial for HUGE numbers'), function () {
76+
.add('new bigFactorial for HUGE numbers', function () {
8277
const res = betterFactorial(new BigNumber(1500))
8378
results.push(res)
8479
})
8580

86-
.add(pad('bigFactorial for "HUGER" numbers'), function () {
81+
.add('bigFactorial for "HUGER" numbers', function () {
8782
const res = bigFactorial(new BigNumber(10000))
8883
results.push(res)
8984
})
90-
.add(pad('new bigFactorial for "HUGER" numbers'), function () {
85+
.add('new bigFactorial for "HUGER" numbers', function () {
9186
const res = betterFactorial(new BigNumber(10000))
9287
results.push(res)
9388
})
94-
.on('cycle', function (event) {
95-
console.log(String(event.target))
96-
})
97-
.on('complete', function () {
98-
})
99-
.run()
89+
90+
bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
91+
await bench.run()

0 commit comments

Comments
 (0)