chore: update dependencies and switch to current mathjs tinybench

This commit is contained in:
Glen Whitney 2025-03-18 22:04:15 -07:00
parent f1a2d04f4b
commit 38160464fa
6 changed files with 618 additions and 401 deletions

View file

@ -0,0 +1,31 @@
const durationWidth = 10
const varianceWidth = 8
/**
* Format a result like "Task name 2.30 µs ±0.79%"
* @param {import('tinybench').Bench} bench
* @param {import('tinybench').Task} task
* @return {string}
*/
export function formatTaskResult (bench, task) {
const nameWidth = Math.max(...bench.tasks.map(task => task.name.length)) + 1
const name = task.name
const { variance, mean } = task.result.latency
const meanStr = `${(mean * 1000).toFixed(2)} \u00b5s`
const varianceStr = `±${((variance / mean) * 100).toFixed(2)}%`
return `${padRight(name, nameWidth)} ${padLeft(meanStr, durationWidth)} ${padLeft(varianceStr, varianceWidth)}`
}
function padRight (text, len, char = ' ') {
const add = Math.max(len - text.length, 0)
return text + char.repeat(add)
}
function padLeft (text, len, char = ' ') {
const add = Math.max(len - text.length, 0)
return char.repeat(add) + text
}

View file

@ -1,5 +1,6 @@
import Benchmark from 'benchmark'
import {Bench} from 'tinybench'
import padRight from 'pad-right'
import { formatTaskResult } from './formatTaskResult.js'
import math from '../src/pocomath.mjs'
function pad (text) {
@ -29,15 +30,14 @@ console.log('polynomials (with coefficients <=', maxCoeff, ')')
const results = []
const suite = new Benchmark.Suite()
suite
const bench = new Bench({time: 100, iterations: 100})
.add(pad('count roots'), function () {
const res = countRoots()
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()