tinybench
Advanced tools
Comparing version 2.5.0 to 2.5.1
@@ -347,4 +347,5 @@ /** | ||
declare const hrtimeNow: () => number; | ||
declare const now: () => number; | ||
export { Bench, BenchEvent, BenchEvents, Fn, Hook, Options, Task, TaskEvents, TaskResult, Bench as default, now }; | ||
export { Bench, BenchEvent, BenchEvents, Fn, Hook, Options, Task, TaskEvents, TaskResult, Bench as default, hrtimeNow, now }; |
@@ -1,10 +0,10 @@ | ||
var S = Object.defineProperty; | ||
var H = (n, a, t) => a in n ? S(n, a, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[a] = t; | ||
var i = (n, a, t) => (H(n, typeof a != "symbol" ? a + "" : a, t), t); | ||
var H = Object.defineProperty; | ||
var N = (s, i, t) => i in s ? H(s, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[i] = t; | ||
var r = (s, i, t) => (N(s, typeof i != "symbol" ? i + "" : i, t), t); | ||
// src/event.ts | ||
function r(n, a = null) { | ||
let t = new Event(n); | ||
function a(s, i = null) { | ||
let t = new Event(s); | ||
return Object.defineProperty(t, "task", { | ||
value: a, | ||
value: i, | ||
enumerable: !0, | ||
@@ -17,3 +17,3 @@ writable: !1, | ||
// src/constants.ts | ||
var z = { | ||
var j = { | ||
1: 12.71, | ||
@@ -140,41 +140,83 @@ 2: 4.303, | ||
infinity: 1.96 | ||
}, w = z; | ||
}, E = j; | ||
// src/utils.ts | ||
var P = (n) => n / 1e6, b = () => { | ||
var n; | ||
return typeof ((n = globalThis.process) == null ? void 0 : n.hrtime) == "function" ? P(Number(process.hrtime.bigint())) : performance.now(); | ||
}, F = (n) => n.reduce((a, t) => a + t, 0) / n.length || 0, B = (n, a) => n.reduce((e, s) => e + (s - a) ** 2) / (n.length - 1) || 0, j = (async () => { | ||
}).constructor, O = (n) => n.constructor === j; | ||
var z = (s) => s / 1e6, P = () => z(Number(process.hrtime.bigint())), b = () => performance.now(); | ||
function q(s) { | ||
return s !== null && typeof s == "object" && typeof s.then == "function"; | ||
} | ||
var R = (s) => s.reduce((i, t) => i + t, 0) / s.length || 0, B = (s, i) => s.reduce((e, n) => e + (n - i) ** 2, 0) / (s.length - 1) || 0, V = (async () => { | ||
}).constructor, C = (s) => s.constructor === V, T = async (s) => { | ||
if (C(s.fn)) | ||
return !0; | ||
try { | ||
if (s.opts.beforeEach != null) | ||
try { | ||
await s.opts.beforeEach.call(s); | ||
} catch (e) { | ||
} | ||
let i = s.fn(), t = q(i); | ||
if (t) | ||
try { | ||
await i; | ||
} catch (e) { | ||
} | ||
if (s.opts.afterEach != null) | ||
try { | ||
await s.opts.afterEach.call(s); | ||
} catch (e) { | ||
} | ||
return t; | ||
} catch (i) { | ||
return !1; | ||
} | ||
}; | ||
// src/task.ts | ||
var p = class extends EventTarget { | ||
constructor(t, e, s, o = {}) { | ||
constructor(t, e, n, h = {}) { | ||
super(); | ||
i(this, "bench"); | ||
i(this, "name"); | ||
i(this, "fn"); | ||
i(this, "runs", 0); | ||
i(this, "result"); | ||
i(this, "opts"); | ||
this.bench = t, this.name = e, this.fn = s, this.opts = o; | ||
r(this, "bench"); | ||
r(this, "name"); | ||
r(this, "fn"); | ||
r(this, "runs", 0); | ||
r(this, "result"); | ||
r(this, "opts"); | ||
this.bench = t, this.name = e, this.fn = n, this.opts = h; | ||
} | ||
async run() { | ||
var o, u, l; | ||
this.dispatchEvent(r("start", this)); | ||
let t = 0, e = [], s = O(this.fn); | ||
for (await this.bench.setup(this, "run"), this.opts.beforeAll != null && await this.opts.beforeAll.call(this); (t < this.bench.time || this.runs < this.bench.iterations) && !((o = this.bench.signal) != null && o.aborted); ) { | ||
this.opts.beforeEach != null && await this.opts.beforeEach.call(this); | ||
let h = 0; | ||
var h, c, u, m; | ||
this.dispatchEvent(a("start", this)); | ||
let t = 0, e = []; | ||
if (await this.bench.setup(this, "run"), this.opts.beforeAll != null) | ||
try { | ||
h = this.bench.now(), s ? await this.fn() : this.fn(); | ||
} catch (f) { | ||
this.setResult({ error: f }); | ||
await this.opts.beforeAll.call(this); | ||
} catch (o) { | ||
this.setResult({ error: o }); | ||
} | ||
let c = this.bench.now() - h; | ||
this.runs += 1, e.push(c), t += c, this.opts.afterEach != null && await this.opts.afterEach.call(this); | ||
let n = await T(this); | ||
try { | ||
for (; (t < this.bench.time || this.runs < this.bench.iterations) && !((h = this.bench.signal) != null && h.aborted); ) { | ||
this.opts.beforeEach != null && await this.opts.beforeEach.call(this); | ||
let o = 0; | ||
if (n) { | ||
let l = this.bench.now(); | ||
await this.fn(), o = this.bench.now() - l; | ||
} else { | ||
let l = this.bench.now(); | ||
this.fn(), o = this.bench.now() - l; | ||
} | ||
e.push(o), this.runs += 1, t += o, this.opts.afterEach != null && await this.opts.afterEach.call(this); | ||
} | ||
} catch (o) { | ||
this.setResult({ error: o }); | ||
} | ||
this.opts.afterAll != null && await this.opts.afterAll.call(this), await this.bench.teardown(this, "run"), e.sort((h, c) => h - c); | ||
{ | ||
let h = e[0], c = e[e.length - 1], f = t / this.runs, L = 1e3 / f, E = F(e), g = B(e, E), T = Math.sqrt(g), k = T / Math.sqrt(e.length), y = e.length - 1, x = w[String(Math.round(y) || 1)] || w.infinity, M = k * x, A = M / E * 100 || 0, K = e[Math.ceil(e.length * (75 / 100)) - 1], _ = e[Math.ceil(e.length * (99 / 100)) - 1], R = e[Math.ceil(e.length * (99.5 / 100)) - 1], I = e[Math.ceil(e.length * (99.9 / 100)) - 1]; | ||
if (this.opts.afterAll != null) | ||
try { | ||
await this.opts.afterAll.call(this); | ||
} catch (o) { | ||
this.setResult({ error: o }); | ||
} | ||
if (await this.bench.teardown(this, "run"), e.sort((o, l) => o - l), !((c = this.result) != null && c.error)) { | ||
let o = e[0], l = e[e.length - 1], g = t / this.runs, L = 1e3 / g, w = R(e), k = B(e, w), y = Math.sqrt(k), x = y / Math.sqrt(e.length), M = e.length - 1, A = E[String(Math.round(M) || 1)] || E.infinity, F = x * A, O = F / w * 100 || 0, K = e[Math.ceil(e.length * (75 / 100)) - 1], _ = e[Math.ceil(e.length * (99 / 100)) - 1], S = e[Math.ceil(e.length * (99.5 / 100)) - 1], I = e[Math.ceil(e.length * (99.9 / 100)) - 1]; | ||
if ((u = this.bench.signal) != null && u.aborted) | ||
@@ -184,41 +226,65 @@ return this; | ||
totalTime: t, | ||
min: h, | ||
max: c, | ||
min: o, | ||
max: l, | ||
hz: L, | ||
period: f, | ||
period: g, | ||
samples: e, | ||
mean: E, | ||
variance: g, | ||
sd: T, | ||
sem: k, | ||
df: y, | ||
critical: x, | ||
moe: M, | ||
rme: A, | ||
mean: w, | ||
variance: k, | ||
sd: y, | ||
sem: x, | ||
df: M, | ||
critical: A, | ||
moe: F, | ||
rme: O, | ||
p75: K, | ||
p99: _, | ||
p995: R, | ||
p995: S, | ||
p999: I | ||
}); | ||
} | ||
return (l = this.result) != null && l.error && (this.dispatchEvent(r("error", this)), this.bench.dispatchEvent(r("error", this))), this.dispatchEvent(r("cycle", this)), this.bench.dispatchEvent(r("cycle", this)), this.dispatchEvent(r("complete", this)), this; | ||
return (m = this.result) != null && m.error && (this.dispatchEvent(a("error", this)), this.bench.dispatchEvent(a("error", this))), this.dispatchEvent(a("cycle", this)), this.bench.dispatchEvent(a("cycle", this)), this.dispatchEvent(a("complete", this)), this; | ||
} | ||
async warmup() { | ||
var s; | ||
this.dispatchEvent(r("warmup", this)); | ||
var h; | ||
this.dispatchEvent(a("warmup", this)); | ||
let t = this.bench.now(), e = 0; | ||
for (await this.bench.setup(this, "warmup"); (e < this.bench.warmupTime || this.runs < this.bench.warmupIterations) && !((s = this.bench.signal) != null && s.aborted); ) { | ||
if (await this.bench.setup(this, "warmup"), this.opts.beforeAll != null) | ||
try { | ||
await Promise.resolve().then(this.fn); | ||
} catch (o) { | ||
await this.opts.beforeAll.call(this); | ||
} catch (c) { | ||
this.setResult({ error: c }); | ||
} | ||
this.runs += 1, e = this.bench.now() - t; | ||
let n = await T(this); | ||
for (; (e < this.bench.warmupTime || this.runs < this.bench.warmupIterations) && !((h = this.bench.signal) != null && h.aborted); ) { | ||
if (this.opts.beforeEach != null) | ||
try { | ||
await this.opts.beforeEach.call(this); | ||
} catch (c) { | ||
this.setResult({ error: c }); | ||
} | ||
try { | ||
n ? await this.fn() : this.fn(); | ||
} catch (c) { | ||
} | ||
if (this.runs += 1, e = this.bench.now() - t, this.opts.afterEach != null) | ||
try { | ||
await this.opts.afterEach.call(this); | ||
} catch (c) { | ||
this.setResult({ error: c }); | ||
} | ||
} | ||
if (this.opts.afterAll != null) | ||
try { | ||
await this.opts.afterAll.call(this); | ||
} catch (c) { | ||
this.setResult({ error: c }); | ||
} | ||
this.bench.teardown(this, "warmup"), this.runs = 0; | ||
} | ||
addEventListener(t, e, s) { | ||
super.addEventListener(t, e, s); | ||
addEventListener(t, e, n) { | ||
super.addEventListener(t, e, n); | ||
} | ||
removeEventListener(t, e, s) { | ||
super.removeEventListener(t, e, s); | ||
removeEventListener(t, e, n) { | ||
super.removeEventListener(t, e, n); | ||
} | ||
@@ -229,3 +295,3 @@ setResult(t) { | ||
reset() { | ||
this.dispatchEvent(r("reset", this)), this.runs = 0, this.result = void 0; | ||
this.dispatchEvent(a("reset", this)), this.runs = 0, this.result = void 0; | ||
} | ||
@@ -237,20 +303,20 @@ }; | ||
constructor(t = {}) { | ||
var e, s, o, u, l, h, c; | ||
var e, n, h, c, u, m, o; | ||
super(); | ||
i(this, "_tasks", /* @__PURE__ */ new Map()); | ||
i(this, "_todos", /* @__PURE__ */ new Map()); | ||
i(this, "signal"); | ||
i(this, "warmupTime", 100); | ||
i(this, "warmupIterations", 5); | ||
i(this, "time", 500); | ||
i(this, "iterations", 10); | ||
i(this, "now", b); | ||
i(this, "setup"); | ||
i(this, "teardown"); | ||
this.now = (e = t.now) != null ? e : this.now, this.warmupTime = (s = t.warmupTime) != null ? s : this.warmupTime, this.warmupIterations = (o = t.warmupIterations) != null ? o : this.warmupIterations, this.time = (u = t.time) != null ? u : this.time, this.iterations = (l = t.iterations) != null ? l : this.iterations, this.signal = t.signal, this.setup = (h = t.setup) != null ? h : () => { | ||
}, this.teardown = (c = t.teardown) != null ? c : () => { | ||
r(this, "_tasks", /* @__PURE__ */ new Map()); | ||
r(this, "_todos", /* @__PURE__ */ new Map()); | ||
r(this, "signal"); | ||
r(this, "warmupTime", 100); | ||
r(this, "warmupIterations", 5); | ||
r(this, "time", 500); | ||
r(this, "iterations", 10); | ||
r(this, "now", b); | ||
r(this, "setup"); | ||
r(this, "teardown"); | ||
this.now = (e = t.now) != null ? e : this.now, this.warmupTime = (n = t.warmupTime) != null ? n : this.warmupTime, this.warmupIterations = (h = t.warmupIterations) != null ? h : this.warmupIterations, this.time = (c = t.time) != null ? c : this.time, this.iterations = (u = t.iterations) != null ? u : this.iterations, this.signal = t.signal, this.setup = (m = t.setup) != null ? m : () => { | ||
}, this.teardown = (o = t.teardown) != null ? o : () => { | ||
}, this.signal && this.signal.addEventListener( | ||
"abort", | ||
() => { | ||
this.dispatchEvent(r("abort")); | ||
this.dispatchEvent(a("abort")); | ||
}, | ||
@@ -262,10 +328,10 @@ { once: !0 } | ||
var e; | ||
this.dispatchEvent(r("start")); | ||
this.dispatchEvent(a("start")); | ||
let t = []; | ||
for (let s of [...this._tasks.values()]) | ||
(e = this.signal) != null && e.aborted ? t.push(s) : t.push(await s.run()); | ||
return this.dispatchEvent(r("complete")), t; | ||
for (let n of [...this._tasks.values()]) | ||
(e = this.signal) != null && e.aborted ? t.push(n) : t.push(await n.run()); | ||
return this.dispatchEvent(a("complete")), t; | ||
} | ||
async warmup() { | ||
this.dispatchEvent(r("warmup")); | ||
this.dispatchEvent(a("warmup")); | ||
for (let [, t] of this._tasks) | ||
@@ -275,24 +341,24 @@ await t.warmup(); | ||
reset() { | ||
this.dispatchEvent(r("reset")), this._tasks.forEach((t) => { | ||
this.dispatchEvent(a("reset")), this._tasks.forEach((t) => { | ||
t.reset(); | ||
}); | ||
} | ||
add(t, e, s = {}) { | ||
let o = new p(this, t, e, s); | ||
return this._tasks.set(t, o), this.dispatchEvent(r("add", o)), this; | ||
add(t, e, n = {}) { | ||
let h = new p(this, t, e, n); | ||
return this._tasks.set(t, h), this.dispatchEvent(a("add", h)), this; | ||
} | ||
todo(t, e = () => { | ||
}, s = {}) { | ||
let o = new p(this, t, e, s); | ||
return this._todos.set(t, o), this.dispatchEvent(r("todo", o)), this; | ||
}, n = {}) { | ||
let h = new p(this, t, e, n); | ||
return this._todos.set(t, h), this.dispatchEvent(a("todo", h)), this; | ||
} | ||
remove(t) { | ||
let e = this.getTask(t); | ||
return this.dispatchEvent(r("remove", e)), this._tasks.delete(t), this; | ||
return this.dispatchEvent(a("remove", e)), this._tasks.delete(t), this; | ||
} | ||
addEventListener(t, e, s) { | ||
super.addEventListener(t, e, s); | ||
addEventListener(t, e, n) { | ||
super.addEventListener(t, e, n); | ||
} | ||
removeEventListener(t, e, s) { | ||
super.removeEventListener(t, e, s); | ||
removeEventListener(t, e, n) { | ||
super.removeEventListener(t, e, n); | ||
} | ||
@@ -323,8 +389,9 @@ table() { | ||
// src/index.ts | ||
var tt = v; | ||
var it = v; | ||
export { | ||
v as Bench, | ||
p as Task, | ||
tt as default, | ||
it as default, | ||
P as hrtimeNow, | ||
b as now | ||
}; |
{ | ||
"name": "tinybench", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"type": "module", | ||
"packageManager": "pnpm@7.5.1", | ||
"packageManager": "pnpm@8.4.0", | ||
"main": "./dist/index.cjs", | ||
@@ -20,21 +20,2 @@ "module": "./dist/index.js", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^7.0.4", | ||
"@size-limit/time": "^7.0.8", | ||
"@types/node": "^18.7.13", | ||
"@typescript-eslint/eslint-plugin": "^5.35.1", | ||
"@typescript-eslint/parser": "^5.35.1", | ||
"bumpp": "^8.2.0", | ||
"changelogithub": "^0.12.4", | ||
"clean-publish": "^3.4.4", | ||
"eslint": "^8.22.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"nano-staged": "^0.5.0", | ||
"size-limit": "^7.0.8", | ||
"tsup": "^5.11.7", | ||
"typescript": "^4.5.4", | ||
"vite": "^2.9.12", | ||
"vitest": "^0.14.2" | ||
}, | ||
"keywords": [ | ||
@@ -46,7 +27,4 @@ "benchmark", | ||
"scripts": { | ||
"dev": "tsup --watch", | ||
"build": "tsup", | ||
"release": "bumpp package.json --commit --push --tag && npm run publish", | ||
"test": "vitest --no-threads" | ||
"publish": "npm run build && clean-publish" | ||
} | ||
} | ||
} |
@@ -355,2 +355,11 @@ # tinybench | ||
### `process.hrtime` | ||
if you want more accurate results for nodejs with `process.hrtime`, then import | ||
the `hrtimeNow` function from the library and pass it to the `Bench` options. | ||
```ts | ||
import { hrtimeNow } from 'tinybench'; | ||
``` | ||
It may make your benchmarks slower, check #42. | ||
## Prior art | ||
@@ -357,0 +366,0 @@ |
Sorry, the diff of this file is not supported yet
41638
0
1103
393