Browse Source

benchmark => tinybench

pull/1175/head
Vitaly Puzrin 5 months ago
parent
commit
57a68632ce
  1. 64
      benchmark/benchmark.mjs
  2. 38
      package-lock.json
  3. 3
      package.json

64
benchmark/benchmark.mjs

@ -3,9 +3,7 @@
import fs from 'node:fs'
import util from 'node:util'
import Benchmark from 'benchmark'
import ansi from 'ansi'
const cursor = ansi(process.stdout)
import { Bench } from 'tinybench'
const IMPLS = []
@ -27,34 +25,34 @@ fs.readdirSync(new URL('./samples', import.meta.url)).sort().forEach(sample => {
const title = `(${content.string.length} bytes)`
function onComplete () { cursor.write('\n') }
const suite = new Benchmark.Suite(
title,
{
onStart: () => { console.log('\nSample: %s %s', sample, title) },
onComplete
}
)
const bench = new Bench({ name: title })
IMPLS.forEach(function (impl) {
suite.add(
impl.name,
{
onCycle: function onCycle (event) {
cursor.horizontalAbsolute()
cursor.eraseLine()
cursor.write(' > ' + event.target)
},
onComplete,
fn: function () { impl.code.run(content.string) }
}
)
bench.add(impl.name, function () { impl.code.run(content.string) })
})
SAMPLES.push({ name: sample.split('.')[0], title, content, suite })
SAMPLES.push({ name: sample.split('.')[0], filename: sample, title, content, bench })
})
function formatNumber (num) {
return num.toLocaleString('en-US', { maximumFractionDigits: 0 })
}
function formatTask (task) {
const result = task.result
if (result.state !== 'completed') {
return `${task.name}: ${result.state}`
}
return [
task.name,
`${formatNumber(result.throughput.mean)} ops/sec`,
`+/-${result.throughput.rme.toFixed(2)}%`,
`${result.throughput.samplesCount} samples`
].join(' ')
}
function select (patterns) {
const result = []
@ -77,7 +75,7 @@ function select (patterns) {
return result
}
function run (files) {
async function run (files) {
const selected = select(files)
if (selected.length > 0) {
@ -89,9 +87,15 @@ function run (files) {
console.log('There isn\'t any sample matches any of these patterns: %s', util.inspect(files))
}
selected.forEach(function (sample) {
sample.suite.run()
})
for (const sample of selected) {
console.log('\n\nSample: %s %s', sample.filename, sample.title)
sample.bench.addEventListener('cycle', function (event) {
console.log(' > %s', formatTask(event.task))
})
await sample.bench.run()
}
}
run(process.argv.slice(2).map(source => new RegExp(source, 'i')))
await run(process.argv.slice(2).map(source => new RegExp(source, 'i')))

38
package-lock.json

@ -30,8 +30,6 @@
"markdown-it": "bin/markdown-it.mjs"
},
"devDependencies": {
"ansi": "^0.3.1",
"benchmark": "~2.1.0",
"c8": "^11.0.0",
"eslint": "^9.39.4",
"gh-pages": "^6.3.0",
@ -49,6 +47,7 @@
"ndoc": "^6.0.1",
"neostandard": "^0.13.0",
"terser": "^5.48.0",
"tinybench": "^6.0.2",
"vite": "^8.0.14",
"vite-plugin-singlefile": "^2.3.3"
}
@ -1213,13 +1212,6 @@
"url": "https://github.com/sponsors/epoberezkin"
}
},
"node_modules/ansi": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/ansi/-/ansi-0.3.1.tgz",
"integrity": "sha512-iFY7JCgHbepc0b82yLaw4IMortylNb6wG4kL+4R0C3iv6i+RHGHux/yUX5BTiRvSX/shMnngjR1YyNMnXEFh5A==",
"dev": true,
"license": "MIT"
},
"node_modules/ansi-regex": {
"version": "6.2.2",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.2.2.tgz",
@ -1470,17 +1462,6 @@
"dev": true,
"license": "MIT"
},
"node_modules/benchmark": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/benchmark/-/benchmark-2.1.4.tgz",
"integrity": "sha512-l9MlfN4M1K/H2fbhfMy3B7vJd6AGKJVQn2h6Sg/Yx+KckoUA7ewS5Vv6TjSq18ooE1kS9hhAlQRH3AkXIh/aOQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"lodash": "^4.17.4",
"platform": "^1.3.3"
}
},
"node_modules/brace-expansion": {
"version": "1.1.14",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.14.tgz",
@ -5033,13 +5014,6 @@
"node": ">=8"
}
},
"node_modules/platform": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz",
"integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==",
"dev": true,
"license": "MIT"
},
"node_modules/possible-typed-array-names": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz",
@ -6205,6 +6179,16 @@
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/tinybench": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/tinybench/-/tinybench-6.0.2.tgz",
"integrity": "sha512-FlHoQpcFvCzeXK5kVPvV7IVgW/hs/B36QWTz876iSdeJguBDfdTSRQmYmaHX+fQNt4hp+gEFB2XXw+8hT4/y8A==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=20.0.0"
}
},
"node_modules/tinyglobby": {
"version": "0.2.16",
"resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.16.tgz",

3
package.json

@ -70,8 +70,6 @@
"uc.micro": "^2.1.0"
},
"devDependencies": {
"ansi": "^0.3.1",
"benchmark": "~2.1.0",
"c8": "^11.0.0",
"eslint": "^9.39.4",
"gh-pages": "^6.3.0",
@ -89,6 +87,7 @@
"ndoc": "^6.0.1",
"neostandard": "^0.13.0",
"terser": "^5.48.0",
"tinybench": "^6.0.2",
"vite": "^8.0.14",
"vite-plugin-singlefile": "^2.3.3"
}

Loading…
Cancel
Save