Skip to content

Commit

Permalink
chore: refactor the benchmarks to use tinybench
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Dec 11, 2024
1 parent 7f02ba2 commit 543dfb1
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 255 deletions.
43 changes: 0 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@typescript-eslint/parser": "7.16.1",
"assert": "2.1.0",
"babel-loader": "9.2.1",
"benchmark": "2.1.4",
"c8": "10.1.2",
"codecov": "3.8.3",
"core-js": "3.39.0",
Expand Down Expand Up @@ -86,7 +85,6 @@
"ndarray-ops": "1.2.2",
"ndarray-pack": "1.2.1",
"numericjs": "1.2.6",
"pad-right": "0.2.2",
"prettier": "3.3.3",
"process": "0.11.10",
"sinon": "19.0.2",
Expand Down
26 changes: 9 additions & 17 deletions test/benchmark/algebra.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// test performance of the expression parser in node.js

import Benchmark from 'benchmark'
import padRight from 'pad-right'
import { simplify, derivative } from '../../lib/esm/index.js'

function pad (text) {
return padRight(text, 40, ' ')
}
import { Bench } from 'tinybench'
import { derivative, simplify } from '../../lib/esm/index.js'
import { formatTaskResult } from './utils/formatTaskResult.js'

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

const results = []

const suite = new Benchmark.Suite()
suite
.add(pad('algebra simplify '), function () {
const bench = new Bench({ time: 100, iterations: 100 })
.add('algebra simplify ', function () {
const res = simplify(simplifyExpr)
results.push(res)
})
.add(pad('algebra derivative'), function () {
.add('algebra derivative', function () {
const res = derivative(derivativeExpr, 'x')
results.push(res)
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
})
.run()

bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
await bench.run()
17 changes: 6 additions & 11 deletions test/benchmark/derivative.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// test performance of derivative

import Benchmark from 'benchmark'
import { Bench } from 'tinybench'
import { derivative, parse } from '../../lib/esm/index.js'
import { formatTaskResult } from './utils/formatTaskResult.js'

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

const results = []

Benchmark.options.minSamples = 100

const suite = new Benchmark.Suite()
suite
const bench = new Bench({ time: 100, iterations: 100 })
.add('ddf', function () {
const res = derivative(derivative(expr, parse('x'), { simplify: false }), parse('x'), { simplify: false })
results.splice(0, 1, res)
Expand All @@ -24,9 +22,6 @@ suite
const res = derivative(expr, parse('x'), { simplify: false })
results.splice(0, 1, res)
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
})
.run()

bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
await bench.run()
49 changes: 17 additions & 32 deletions test/benchmark/expression_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
// browserify benchmark/expression_parser.js -o ./benchmark_expression_parser.js

import assert from 'node:assert'
import Benchmark from 'benchmark'
import padRight from 'pad-right'
import { create, all } from '../../lib/esm/index.js'
import { Bench } from 'tinybench'
import { all, create } from '../../lib/esm/index.js'
import { getSafeProperty } from '../../lib/esm/utils/customs.js'
import { formatTaskResult } from './utils/formatTaskResult.js'

const math = create(all)

// expose on window when using bundled in a browser
if (typeof window !== 'undefined') {
window.Benchmark = Benchmark
}

function pad (text) {
return padRight(text, 40, ' ')
}

const expr = '2 + 3 * sin(pi / 4) - 4x'
const scope = new Map([
['x', 2]
Expand Down Expand Up @@ -49,40 +40,34 @@ assertApproxEqual(compiledPlainJs.evaluate(scope), correctResult, 1e-7)
let total = 0
const nodes = []

const suite = new Benchmark.Suite()
suite
.add(pad('(plain js) evaluate'), function () {
const bench = new Bench({ time: 100, iterations: 100 })
.add('(plain js) evaluate', function () {
total += compiledPlainJs.evaluate(scope)
})

.add(pad('(mathjs) evaluate'), function () {
.add('(mathjs) evaluate', function () {
total += compiled.evaluate(scope)
})
.add(pad('(mathjs) parse, compile, evaluate'), function () {
.add('(mathjs) parse, compile, evaluate', function () {
total += math.parse(expr).compile().evaluate(scope)
})
.add(pad('(mathjs) parse, compile'), function () {
.add('(mathjs) parse, compile', function () {
const node = math.parse(expr).compile()
nodes.push(node)
})
.add(pad('(mathjs) parse'), function () {
.add('(mathjs) parse', function () {
const node = math.parse(expr)
nodes.push(node)
})

.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
// we count at total to prevent the browsers from not executing
// the benchmarks ("dead code") when the results would not be used.
if (total > 1e6) {
console.log('')
} else {
console.log('')
}
})
.run()
bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
await bench.run()

// we count at total to prevent the browsers from not executing
// the benchmarks ("dead code") when the results would not be used.
if (total > 1e6) {
console.log('')
}

function assertApproxEqual (actual, expected, tolerance) {
const diff = Math.abs(expected - actual)
Expand Down
40 changes: 16 additions & 24 deletions test/benchmark/factorial.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Benchmark from 'benchmark'
import padRight from 'pad-right'
import BigNumber from 'decimal.js'

function pad (text) {
return padRight(text, 40, ' ')
}
import { Bench } from 'tinybench'
import { formatTaskResult } from './utils/formatTaskResult.js'

const results = []

Expand Down Expand Up @@ -45,55 +41,51 @@ function betterFactorial (n) {
return prod
}

const suite = new Benchmark.Suite()
suite
.add(pad('bigFactorial for small numbers'), function () {
const bench = new Bench({ time: 100, iterations: 100 })
.add('bigFactorial for small numbers', function () {
const res = bigFactorial(new BigNumber(8))
results.push(res)
})
.add(pad('new bigFactorial for small numbers'), function () {
.add('new bigFactorial for small numbers', function () {
const res = betterFactorial(new BigNumber(8))
results.push(res)
})

.add(pad('bigFactorial for small numbers 2'), function () {
.add('bigFactorial for small numbers 2', function () {
const res = bigFactorial(new BigNumber(20))
results.push(res)
})
.add(pad('new bigFactorial for small numbers 2'), function () {
.add('new bigFactorial for small numbers 2', function () {
const res = betterFactorial(new BigNumber(20))
results.push(res)
})

.add(pad('bigFactorial for big numbers'), function () {
.add('bigFactorial for big numbers', function () {
const res = bigFactorial(new BigNumber(600))
results.push(res)
})
.add(pad('new bigFactorial for big numbers'), function () {
.add('new bigFactorial for big numbers', function () {
const res = betterFactorial(new BigNumber(600))
results.push(res)
})

.add(pad('bigFactorial for HUGE numbers'), function () {
.add('bigFactorial for HUGE numbers', function () {
const res = bigFactorial(new BigNumber(1500))
results.push(res)
})
.add(pad('new bigFactorial for HUGE numbers'), function () {
.add('new bigFactorial for HUGE numbers', function () {
const res = betterFactorial(new BigNumber(1500))
results.push(res)
})

.add(pad('bigFactorial for "HUGER" numbers'), function () {
.add('bigFactorial for "HUGER" numbers', function () {
const res = bigFactorial(new BigNumber(10000))
results.push(res)
})
.add(pad('new bigFactorial for "HUGER" numbers'), function () {
.add('new bigFactorial for "HUGER" numbers', function () {
const res = betterFactorial(new BigNumber(10000))
results.push(res)
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
})
.run()

bench.addEventListener('cycle', (event) => console.log(formatTaskResult(bench, event.task)))
await bench.run()
Loading

0 comments on commit 543dfb1

Please sign in to comment.