New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tinybench

Package Overview
Dependencies
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tinybench - npm Package Compare versions

Comparing version

to
3.1.0

19

dist/index.d.ts

@@ -49,2 +49,8 @@ /**

/**
* run the current task and write the results in `Task.result` object property
* @returns the current task
* @internal
*/
runSync(): this;
/**
* warmup the current task

@@ -54,3 +60,9 @@ * @internal

warmup(): Promise<void>;
/**
* warmup the current task (sync version)
* @internal
*/
warmupSync(): void;
private benchmark;
private benchmarkSync;
/**

@@ -61,2 +73,4 @@ * merge into the result object values

private mergeTaskResult;
private postWarmup;
private processRunResult;
}

@@ -489,2 +503,3 @@

run(): Promise<Task[]>;
runSync(): Task[];
/**

@@ -500,4 +515,8 @@ * table of the tasks results

private warmupTasks;
/**
* warmup the benchmark tasks (sync version)
*/
private warmupTasksSync;
}
export { Bench, type BenchEvent, type BenchEvents, type BenchEventsMap, type BenchOptions, type EventListener, type Fn, type FnOptions, type Hook, JSRuntime, type Statistics, Task, type TaskEvents, type TaskEventsMap, type TaskResult, hrtimeNow, nToMs, now };

595

dist/index.js

@@ -1,14 +0,14 @@

var l1 = Object.defineProperty;
var z = (t) => {
throw TypeError(t);
var c1 = Object.defineProperty;
var q = (e) => {
throw TypeError(e);
};
var m1 = (t, s, e) => s in t ? l1(t, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[s] = e;
var A = (t, s, e) => m1(t, typeof s != "symbol" ? s + "" : s, e), V = (t, s, e) => s.has(t) || z("Cannot " + e);
var d = (t, s, e) => (V(t, s, "read from private field"), e ? e.call(t) : s.get(t)), S = (t, s, e) => s.has(t) ? z("Cannot add the same private member more than once") : s instanceof WeakSet ? s.add(t) : s.set(t, e), f = (t, s, e, n) => (V(t, s, "write to private field"), n ? n.call(t, e) : s.set(t, e), e);
var B = (t, s, e, n) => ({
var h1 = (e, s, t) => s in e ? c1(e, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[s] = t;
var M = (e, s, t) => h1(e, typeof s != "symbol" ? s + "" : s, t), K = (e, s, t) => s.has(e) || q("Cannot " + t);
var f = (e, s, t) => (K(e, s, "read from private field"), t ? t.call(e) : s.get(e)), F = (e, s, t) => s.has(e) ? q("Cannot add the same private member more than once") : s instanceof WeakSet ? s.add(e) : s.set(e, t), b = (e, s, t, n) => (K(e, s, "write to private field"), n ? n.call(e, t) : s.set(e, t), t);
var P = (e, s, t, n) => ({
set _(r) {
f(t, s, r, e);
b(e, s, r, t);
},
get _() {
return d(t, s, n);
return f(e, s, n);
}

@@ -18,36 +18,36 @@ });

// node_modules/.pnpm/yocto-queue@1.1.1/node_modules/yocto-queue/index.js
var P = class {
var B = class {
constructor(s) {
A(this, "value");
A(this, "next");
M(this, "value");
M(this, "next");
this.value = s;
}
}, p, v, y, E = class {
}, p, v, w, E = class {
constructor() {
S(this, p);
S(this, v);
S(this, y);
F(this, p);
F(this, v);
F(this, w);
this.clear();
}
enqueue(s) {
let e = new P(s);
d(this, p) ? (d(this, v).next = e, f(this, v, e)) : (f(this, p, e), f(this, v, e)), B(this, y)._++;
let t = new B(s);
f(this, p) ? (f(this, v).next = t, b(this, v, t)) : (b(this, p, t), b(this, v, t)), P(this, w)._++;
}
dequeue() {
let s = d(this, p);
let s = f(this, p);
if (s)
return f(this, p, d(this, p).next), B(this, y)._--, s.value;
return b(this, p, f(this, p).next), P(this, w)._--, s.value;
}
peek() {
if (d(this, p))
return d(this, p).value;
if (f(this, p))
return f(this, p).value;
}
clear() {
f(this, p, void 0), f(this, v, void 0), f(this, y, 0);
b(this, p, void 0), b(this, v, void 0), b(this, w, 0);
}
get size() {
return d(this, y);
return f(this, w);
}
*[Symbol.iterator]() {
let s = d(this, p);
let s = f(this, p);
for (; s; )

@@ -57,31 +57,31 @@ yield s.value, s = s.next;

};
p = new WeakMap(), v = new WeakMap(), y = new WeakMap();
p = new WeakMap(), v = new WeakMap(), w = new WeakMap();
// node_modules/.pnpm/p-limit@6.1.0/node_modules/p-limit/index.js
function w(t) {
C(t);
let s = new E(), e = 0, n = () => {
e < t && s.size > 0 && (s.dequeue()(), e++);
// node_modules/.pnpm/p-limit@6.2.0/node_modules/p-limit/index.js
function k(e) {
z(e);
let s = new E(), t = 0, n = () => {
t < e && s.size > 0 && (s.dequeue()(), t++);
}, r = () => {
e--, n();
}, o = async (h, i, u) => {
let a = (async () => h(...u))();
i(a);
t--, n();
}, a = async (h, i, c) => {
let g = (async () => h(...c))();
i(g);
try {
await a;
} catch (q) {
await g;
} catch (L1) {
}
r();
}, l = (h, i, u) => {
new Promise((a) => {
s.enqueue(a);
}, o = (h, i, c) => {
new Promise((g) => {
s.enqueue(g);
}).then(
o.bind(void 0, h, i, u)
), (async () => (await Promise.resolve(), e < t && n()))();
}, c = (h, ...i) => new Promise((u) => {
l(h, u, i);
a.bind(void 0, h, i, c)
), (async () => (await Promise.resolve(), t < e && n()))();
}, u = (h, ...i) => new Promise((c) => {
o(h, c, i);
});
return Object.defineProperties(c, {
return Object.defineProperties(u, {
activeCount: {
get: () => e
get: () => t
},

@@ -97,6 +97,6 @@ pendingCount: {

concurrency: {
get: () => t,
get: () => e,
set(h) {
C(h), t = h, queueMicrotask(() => {
for (; e < t && s.size > 0; )
z(h), e = h, queueMicrotask(() => {
for (; t < e && s.size > 0; )
n();

@@ -106,6 +106,6 @@ });

}
}), c;
}), u;
}
function C(t) {
if (!((Number.isInteger(t) || t === Number.POSITIVE_INFINITY) && t > 0))
function z(e) {
if (!((Number.isInteger(e) || e === Number.POSITIVE_INFINITY) && e > 0))
throw new TypeError("Expected `concurrency` to be a number from 1 and up");

@@ -1141,9 +1141,9 @@ }

infinity: 1.96
}), D = 1e3, W = 64, H = 250, Q = 16, T = Object.freeze(() => {
}), C = 1e3, V = 64, W = 250, D = 16, T = Object.freeze(() => {
});
// src/event.ts
var m = (t, s) => {
let e = new Event(t);
return s && Object.defineProperty(e, "task", {
var l = (e, s) => {
let t = new Event(e);
return s && Object.defineProperty(t, "task", {
configurable: !1,

@@ -1153,11 +1153,11 @@ enumerable: !0,

writable: !1
}), e;
}, x = (t, s) => {
let e = new Event("error");
return Object.defineProperty(e, "task", {
}), t;
}, x = (e, s) => {
let t = new Event("error");
return Object.defineProperty(t, "task", {
configurable: !1,
enumerable: !0,
value: t,
value: e,
writable: !1
}), Object.defineProperty(e, "error", {
}), Object.defineProperty(t, "error", {
configurable: !1,

@@ -1167,10 +1167,10 @@ enumerable: !0,

writable: !1
}), e;
}), t;
};
// src/utils.ts
var U, X, p1 = !!globalThis.Bun || !!((X = (U = globalThis.process) == null ? void 0 : U.versions) != null && X.bun), d1 = !!globalThis.Deno, Z, e1, f1 = ((e1 = (Z = globalThis.process) == null ? void 0 : Z.release) == null ? void 0 : e1.name) === "node", b1 = !!globalThis.HermesInternal, t1, g1 = (
var Y, G, l1 = !!globalThis.Bun || !!((G = (Y = globalThis.process) == null ? void 0 : Y.versions) != null && G.bun), m1 = !!globalThis.Deno, U, X, p1 = ((X = (U = globalThis.process) == null ? void 0 : U.release) == null ? void 0 : X.name) === "node", d1 = !!globalThis.HermesInternal, Z, f1 = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
((t1 = globalThis.navigator) == null ? void 0 : t1.userAgent) === "Cloudflare-Workers"
), n1, L, s1, M, r1, v1 = !!((r1 = (M = (s1 = (L = (n1 = globalThis.navigator) == null ? void 0 : n1.userAgent) == null ? void 0 : L.toLowerCase) == null ? void 0 : s1.call(L)) == null ? void 0 : M.includes) != null && r1.call(M, "quickjs-ng")), y1 = typeof globalThis.Netlify == "object", w1 = typeof globalThis.EdgeRuntime == "string", T1 = !!globalThis.__lagon__, k1 = !!globalThis.fastly, E1 = (
((Z = globalThis.navigator) == null ? void 0 : Z.userAgent) === "Cloudflare-Workers"
), t1, A, e1, L, n1, b1 = !!((n1 = (L = (e1 = (A = (t1 = globalThis.navigator) == null ? void 0 : t1.userAgent) == null ? void 0 : A.toLowerCase) == null ? void 0 : e1.call(A)) == null ? void 0 : L.includes) != null && n1.call(L, "quickjs-ng")), y1 = typeof globalThis.Netlify == "object", g1 = typeof globalThis.EdgeRuntime == "string", v1 = !!globalThis.__lagon__, w1 = !!globalThis.fastly, k1 = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access

@@ -1180,49 +1180,49 @@ !!globalThis.$262 && // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access

!!globalThis.AsyncDisposableStack
), x1 = !!globalThis.d8, i1, R1 = (
), T1 = !!globalThis.d8, s1, E1 = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
!!globalThis.inIon && !!((i1 = globalThis.performance) != null && i1.mozMemory)
), O1 = (
!!globalThis.inIon && !!((s1 = globalThis.performance) != null && s1.mozMemory)
), x1 = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
!!globalThis.$ && // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @cspell/spellchecker
"IsHTMLDDA" in globalThis.$
), F1 = (
), S1 = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
!!globalThis.window && !!globalThis.navigator
), g = p1 ? "bun" : d1 ? "deno" : f1 ? "node" : b1 ? "hermes" : y1 ? "netlify" : w1 ? "edge-light" : T1 ? "lagon" : k1 ? "fastly" : g1 ? "workerd" : v1 ? "quickjs-ng" : E1 ? "moddable" : x1 ? "v8" : R1 ? "spidermonkey" : O1 ? "jsc" : F1 ? "browser" : "unknown", u1 = (() => {
var t, s, e, n, r, o, l, c, h, i, u, a;
return g === "bun" ? (t = globalThis.Bun) == null ? void 0 : t.version : g === "deno" ? (e = (s = globalThis.Deno) == null ? void 0 : s.version) == null ? void 0 : e.deno : g === "node" ? (r = (n = globalThis.process) == null ? void 0 : n.versions) == null ? void 0 : r.node : g === "hermes" ? (c = (l = (o = globalThis.HermesInternal) == null ? void 0 : o.getRuntimeProperties) == null ? void 0 : l.call(o)) == null ? void 0 : c[
), y = l1 ? "bun" : m1 ? "deno" : p1 ? "node" : d1 ? "hermes" : y1 ? "netlify" : g1 ? "edge-light" : v1 ? "lagon" : w1 ? "fastly" : f1 ? "workerd" : b1 ? "quickjs-ng" : k1 ? "moddable" : T1 ? "v8" : E1 ? "spidermonkey" : x1 ? "jsc" : S1 ? "browser" : "unknown", o1 = (() => {
var e, s, t, n, r, a, o, u, h, i, c, g;
return y === "bun" ? (e = globalThis.Bun) == null ? void 0 : e.version : y === "deno" ? (t = (s = globalThis.Deno) == null ? void 0 : s.version) == null ? void 0 : t.deno : y === "node" ? (r = (n = globalThis.process) == null ? void 0 : n.versions) == null ? void 0 : r.node : y === "hermes" ? (u = (o = (a = globalThis.HermesInternal) == null ? void 0 : a.getRuntimeProperties) == null ? void 0 : o.call(a)) == null ? void 0 : u[
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
"OSS Release Version"
] : g === "v8" ? (h = globalThis.version) == null ? void 0 : h.call(globalThis) : g === "quickjs-ng" ? (a = (u = (i = globalThis.navigator) == null ? void 0 : i.userAgent) == null ? void 0 : u.split) == null ? void 0 : a.call(u, "/")[1] : "unknown";
})(), h1 = (t) => t / 1e6, O = (t) => t * 1e6, N, o1, a1;
typeof ((a1 = (o1 = globalThis.process) == null ? void 0 : o1.hrtime) == null ? void 0 : a1.bigint) == "function" ? N = globalThis.process.hrtime.bigint.bind(process.hrtime) : N = () => {
] : y === "v8" ? (h = globalThis.version) == null ? void 0 : h.call(globalThis) : y === "quickjs-ng" ? (g = (c = (i = globalThis.navigator) == null ? void 0 : i.userAgent) == null ? void 0 : c.split) == null ? void 0 : g.call(c, "/")[1] : "unknown";
})(), a1 = (e) => e / 1e6, R = (e) => e * 1e6, _, r1, i1;
typeof ((i1 = (r1 = globalThis.process) == null ? void 0 : r1.hrtime) == null ? void 0 : i1.bigint) == "function" ? _ = globalThis.process.hrtime.bigint.bind(process.hrtime) : _ = () => {
throw new Error("hrtime.bigint() is not supported in this JS environment");
};
var S1 = () => h1(Number(N())), L1 = performance.now.bind(performance), j = L1, M1 = (t) => t !== null && typeof t == "object" && typeof t.then == "function", A1 = (t) => (
var R1 = () => a1(Number(_())), O1 = performance.now.bind(performance), j = O1, d = (e) => e !== null && typeof e == "object" && typeof e.then == "function", F1 = (e) => (
// eslint-disable-next-line @typescript-eslint/no-empty-function
(t == null ? void 0 : t.constructor) === (async () => {
(e == null ? void 0 : e.constructor) === (async () => {
}).constructor
), c1 = (t) => {
), u1 = (e) => {
var s;
if (t == null)
if (e == null)
return !1;
if (A1(t))
if (F1(e))
return !0;
try {
let e = t(), n = M1(e);
let t = e(), n = d(t);
if (n)
try {
(s = e.then(T)) == null || s.catch(T);
(s = t.then(T)) == null || s.catch(T);
} catch (r) {
}
return n;
} catch (e) {
} catch (t) {
return !1;
}
}, _ = (t) => {
if (t.length === 0)
}, N = (e) => {
if (e.length === 0)
throw new Error("samples must not be empty");
return t.reduce((s, e) => s + e, 0) / t.length || 0;
}, B1 = (t, s = _(t)) => t.reduce((n, r) => n + (r - s) ** 2, 0) / (t.length - 1) || 0, R = (t, s) => {
if (t.length === 0)
return e.reduce((s, t) => s + t, 0) / e.length || 0;
}, A1 = (e, s = N(e)) => e.reduce((n, r) => n + (r - s) ** 2, 0) / (e.length - 1) || 0, S = (e, s) => {
if (e.length === 0)
throw new Error("samples must not be empty");

@@ -1232,45 +1232,48 @@ if (s < 0 || s > 1)

if (s === 0)
return t[0];
return e[0];
if (s === 1)
return t[t.length - 1];
let e = (t.length - 1) * s, n = Math.floor(e);
return t[n + 1] != null ? (
return e[e.length - 1];
let t = (e.length - 1) * s, n = Math.floor(t);
return e[n + 1] != null ? (
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
t[n] + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(e - n) * (t[n + 1] - t[n])
) : t[n];
}, Y = (t) => R(t, 0.5), G = (t, s, e = s(t)) => {
e[n] + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(t - n) * (e[n + 1] - e[n])
) : e[n];
}, H = (e) => S(e, 0.5), Q = (e, s, t = s(e)) => {
let n = [];
for (let r of t)
n.push(Math.abs(r - e));
for (let r of e)
n.push(Math.abs(r - t));
return s(n);
}, $ = (t) => {
let s = _(t), e = B1(t, s), n = Math.sqrt(e), r = n / Math.sqrt(t.length), o = t.length - 1, l = I[(Math.round(o) || 1).toString()] || I.infinity, c = r * l, h = c / s * 100, i = Y(t);
}, $ = (e) => {
let s = N(e), t = A1(e, s), n = Math.sqrt(t), r = n / Math.sqrt(e.length), a = e.length - 1, o = I[(Math.round(a) || 1).toString()] || I.infinity, u = r * o, h = u / s * 100, i = H(e);
return {
aad: G(t, _, s),
critical: l,
df: o,
mad: G(t, Y, i),
aad: Q(e, N, s),
critical: o,
df: a,
mad: Q(e, H, i),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
max: t[o],
max: e[a],
mean: s,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
min: t[0],
moe: c,
min: e[0],
moe: u,
p50: i,
p75: R(t, 0.75),
p99: R(t, 0.99),
p995: R(t, 0.995),
p999: R(t, 0.999),
p75: S(e, 0.75),
p99: S(e, 0.99),
p995: S(e, 0.995),
p999: S(e, 0.999),
rme: h,
samples: t,
samples: e,
sd: n,
sem: r,
variance: e
variance: t
};
}, m = (e, s) => {
if (!e)
throw new Error(s);
};
// src/task.ts
var F = class extends EventTarget {
constructor(e, n, r, o = {}) {
var O = class extends EventTarget {
constructor(t, n, r, a = {}) {
super();

@@ -1281,9 +1284,9 @@ /**

this.runs = 0;
this.bench = e, this.name = n, this.fn = r, this.fnOpts = o, this.async = c1(r);
this.bench = t, this.name = n, this.fn = r, this.fnOpts = a, this.async = u1(r);
}
addEventListener(e, n, r) {
super.addEventListener(e, n, r);
addEventListener(t, n, r) {
super.addEventListener(t, n, r);
}
removeEventListener(e, n, r) {
super.removeEventListener(e, n, r);
removeEventListener(t, n, r) {
super.removeEventListener(t, n, r);
}

@@ -1295,3 +1298,3 @@ /**

reset() {
this.dispatchEvent(m("reset", this)), this.runs = 0, this.result = void 0;
this.dispatchEvent(l("reset", this)), this.runs = 0, this.result = void 0;
}

@@ -1304,7 +1307,7 @@ /**

async run() {
var r, o, l, c, h, i;
var r, a, o, u, h;
if ((r = this.result) != null && r.error)
return this;
this.dispatchEvent(m("start", this)), await ((l = (o = this.bench.opts).setup) == null ? void 0 : l.call(o, this, "run"));
let { error: e, samples: n } = await this.benchmark(
this.dispatchEvent(l("start", this)), await ((o = (a = this.bench.opts).setup) == null ? void 0 : o.call(a, this, "run"));
let { error: t, samples: n } = await this.benchmark(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

@@ -1315,41 +1318,34 @@ this.bench.opts.time,

);
if (await ((h = (c = this.bench.opts).teardown) == null ? void 0 : h.call(c, this, "run")), n) {
this.runs = n.length;
let u = n.reduce((b, k) => b + k, 0), a = $(
n.sort((b, k) => b - k)
), q = n.map(
(b) => b !== 0 ? 1e3 / b : 1e3 / a.mean
).sort((b, k) => b - k), K = $(q);
if ((i = this.bench.opts.signal) != null && i.aborted)
return this;
this.mergeTaskResult({
critical: a.critical,
df: a.df,
hz: K.mean,
latency: a,
max: a.max,
mean: a.mean,
min: a.min,
moe: a.moe,
p75: a.p75,
p99: a.p99,
p995: a.p995,
p999: a.p999,
period: u / this.runs,
rme: a.rme,
runtime: this.bench.runtime,
runtimeVersion: this.bench.runtimeVersion,
samples: a.samples,
sd: a.sd,
sem: a.sem,
throughput: K,
totalTime: u,
variance: a.variance
});
}
if (e && (this.mergeTaskResult({ error: e }), this.dispatchEvent(x(this, e)), this.bench.dispatchEvent(x(this, e)), this.bench.opts.throws))
throw e;
return this.dispatchEvent(m("cycle", this)), this.bench.dispatchEvent(m("cycle", this)), this.dispatchEvent(m("complete", this)), this;
return await ((h = (u = this.bench.opts).teardown) == null ? void 0 : h.call(u, this, "run")), this.processRunResult({ error: t, latencySamples: n }), this;
}
/**
* run the current task and write the results in `Task.result` object property
* @returns the current task
* @internal
*/
runSync() {
var o, u, h, i, c;
if ((o = this.result) != null && o.error)
return this;
m(
this.bench.concurrency === null,
"Cannot use `concurrency` option when using `runSync`"
), this.dispatchEvent(l("start", this));
let t = (h = (u = this.bench.opts).setup) == null ? void 0 : h.call(u, this, "run");
m(
!d(t),
"`setup` function must be sync when using `runSync()`"
);
let { error: n, samples: r } = this.benchmarkSync(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.bench.opts.time,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.bench.opts.iterations
), a = (c = (i = this.bench.opts).teardown) == null ? void 0 : c.call(i, this, "run");
return m(
!d(a),
"`teardown` function must be sync when using `runSync()`"
), this.processRunResult({ error: n, latencySamples: r }), this;
}
/**
* warmup the current task

@@ -1359,7 +1355,7 @@ * @internal

async warmup() {
var n, r, o, l, c;
var n, r, a, o, u;
if ((n = this.result) != null && n.error)
return;
this.dispatchEvent(m("warmup", this)), await ((o = (r = this.bench.opts).setup) == null ? void 0 : o.call(r, this, "warmup"));
let { error: e } = await this.benchmark(
this.dispatchEvent(l("warmup", this)), await ((a = (r = this.bench.opts).setup) == null ? void 0 : a.call(r, this, "warmup"));
let { error: t } = await this.benchmark(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

@@ -1370,7 +1366,31 @@ this.bench.opts.warmupTime,

);
if (await ((c = (l = this.bench.opts).teardown) == null ? void 0 : c.call(l, this, "warmup")), e && (this.mergeTaskResult({ error: e }), this.dispatchEvent(x(this, e)), this.bench.dispatchEvent(x(this, e)), this.bench.opts.throws))
throw e;
await ((u = (o = this.bench.opts).teardown) == null ? void 0 : u.call(o, this, "warmup")), this.postWarmup(t);
}
async benchmark(e, n) {
var c, h;
/**
* warmup the current task (sync version)
* @internal
*/
warmupSync() {
var a, o, u, h, i;
if ((a = this.result) != null && a.error)
return;
this.dispatchEvent(l("warmup", this));
let t = (u = (o = this.bench.opts).setup) == null ? void 0 : u.call(o, this, "warmup");
m(
!d(t),
"`setup` function must be sync when using `runSync()`"
);
let { error: n } = this.benchmarkSync(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.bench.opts.warmupTime,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.bench.opts.warmupIterations
), r = (i = (h = this.bench.opts).teardown) == null ? void 0 : i.call(h, this, "warmup");
m(
!d(r),
"`teardown` function must be sync when using `runSync()`"
), this.postWarmup(n);
}
async benchmark(t, n) {
var u, h;
if (this.fnOpts.beforeAll != null)

@@ -1382,23 +1402,23 @@ try {

}
let r = 0, o = [], l = async () => {
let r = 0, a = [], o = async () => {
this.fnOpts.beforeEach != null && await this.fnOpts.beforeEach.call(this);
let i = 0;
if (this.async) {
let u = this.bench.opts.now();
await this.fn.call(this), i = this.bench.opts.now() - u;
let c = this.bench.opts.now();
await this.fn.call(this), i = this.bench.opts.now() - c;
} else {
let u = this.bench.opts.now();
this.fn.call(this), i = this.bench.opts.now() - u;
let c = this.bench.opts.now();
this.fn.call(this), i = this.bench.opts.now() - c;
}
o.push(i), r += i, this.fnOpts.afterEach != null && await this.fnOpts.afterEach.call(this);
a.push(i), r += i, this.fnOpts.afterEach != null && await this.fnOpts.afterEach.call(this);
};
try {
let i = w(this.bench.threshold), u = [];
let i = k(this.bench.threshold), c = [];
for (
;
// eslint-disable-next-line no-unmodified-loop-condition
(r < e || o.length + i.activeCount + i.pendingCount < n) && !((c = this.bench.opts.signal) != null && c.aborted);
(r < t || a.length + i.activeCount + i.pendingCount < n) && !((u = this.bench.opts.signal) != null && u.aborted);
)
this.bench.concurrency === "task" ? u.push(i(l)) : await l();
!((h = this.bench.opts.signal) != null && h.aborted) && u.length > 0 && await Promise.all(u);
this.bench.concurrency === "task" ? c.push(i(o)) : await o();
!((h = this.bench.opts.signal) != null && h.aborted) && c.length > 0 && await Promise.all(c);
} catch (i) {

@@ -1413,4 +1433,57 @@ return { error: i };

}
return { samples: o };
return { samples: a };
}
benchmarkSync(t, n) {
if (this.fnOpts.beforeAll != null)
try {
let u = this.fnOpts.beforeAll.call(this);
m(
!d(u),
"`beforeAll` function must be sync when using `runSync()`"
);
} catch (u) {
return { error: u };
}
let r = 0, a = [], o = () => {
if (this.fnOpts.beforeEach != null) {
let c = this.fnOpts.beforeEach.call(this);
m(
!d(c),
"`beforeEach` function must be sync when using `runSync()`"
);
}
let u = 0, h = this.bench.opts.now(), i = this.fn.call(this);
if (u = this.bench.opts.now() - h, m(
!d(i),
"task function must be sync when using `runSync()`"
), a.push(u), r += u, this.fnOpts.afterEach != null) {
let c = this.fnOpts.afterEach.call(this);
m(
!d(c),
"`afterEach` function must be sync when using `runSync()`"
);
}
};
try {
for (
;
// eslint-disable-next-line no-unmodified-loop-condition
r < t || a.length < n;
)
o();
} catch (u) {
return { error: u };
}
if (this.fnOpts.afterAll != null)
try {
let u = this.fnOpts.afterAll.call(this);
m(
!d(u),
"`afterAll` function must be sync when using `runSync()`"
);
} catch (u) {
return { error: u };
}
return { samples: a };
}
/**

@@ -1420,8 +1493,55 @@ * merge into the result object values

*/
mergeTaskResult(e) {
mergeTaskResult(t) {
this.result = Object.freeze({
...this.result,
...e
...t
});
}
postWarmup(t) {
if (t && (this.mergeTaskResult({ error: t }), this.dispatchEvent(x(this, t)), this.bench.dispatchEvent(x(this, t)), this.bench.opts.throws))
throw t;
}
processRunResult({
error: t,
latencySamples: n
}) {
var r;
if (n) {
this.runs = n.length;
let a = n.reduce((i, c) => i + c, 0), o = $(
n.sort((i, c) => i - c)
), u = n.map(
(i) => i !== 0 ? 1e3 / i : 1e3 / o.mean
).sort((i, c) => i - c), h = $(u);
if ((r = this.bench.opts.signal) != null && r.aborted)
return;
this.mergeTaskResult({
critical: o.critical,
df: o.df,
hz: h.mean,
latency: o,
max: o.max,
mean: o.mean,
min: o.min,
moe: o.moe,
p75: o.p75,
p99: o.p99,
p995: o.p995,
p999: o.p999,
period: a / this.runs,
rme: o.rme,
runtime: this.bench.runtime,
runtimeVersion: this.bench.runtimeVersion,
samples: o.samples,
sd: o.sd,
sem: o.sem,
throughput: h,
totalTime: a,
variance: o.variance
});
}
if (t && (this.mergeTaskResult({ error: t }), this.dispatchEvent(x(this, t)), this.bench.dispatchEvent(x(this, t)), this.bench.opts.throws))
throw t;
this.dispatchEvent(l("cycle", this)), this.bench.dispatchEvent(l("cycle", this)), this.dispatchEvent(l("complete", this));
}
};

@@ -1431,3 +1551,3 @@

var J = class extends EventTarget {
constructor(e = {}) {
constructor(t = {}) {
super();

@@ -1450,4 +1570,4 @@ /**

this._tasks = /* @__PURE__ */ new Map();
this.name = e.name, delete e.name, this.runtime = g, this.runtimeVersion = u1, this.opts = {
iterations: W,
this.name = t.name, delete t.name, this.runtime = y, this.runtimeVersion = o1, this.opts = {
iterations: V,
now: j,

@@ -1457,11 +1577,11 @@ setup: T,

throws: !1,
time: D,
time: C,
warmup: !0,
warmupIterations: Q,
warmupTime: H,
...e
warmupIterations: D,
warmupTime: W,
...t
}, this.opts.signal && this.opts.signal.addEventListener(
"abort",
() => {
this.dispatchEvent(m("abort"));
this.dispatchEvent(l("abort"));
},

@@ -1476,3 +1596,3 @@ { once: !0 }

get results() {
return [...this._tasks.values()].map((e) => e.result);
return [...this._tasks.values()].map((t) => t.result);
}

@@ -1494,13 +1614,13 @@ /**

*/
add(e, n, r = {}) {
if (this._tasks.has(e))
throw new Error(`Task "${e}" already exists`);
add(t, n, r = {}) {
if (this._tasks.has(t))
throw new Error(`Task "${t}" already exists`);
{
let o = new F(this, e, n, r);
this._tasks.set(e, o), this.dispatchEvent(m("add", o));
let a = new O(this, t, n, r);
this._tasks.set(t, a), this.dispatchEvent(l("add", a));
}
return this;
}
addEventListener(e, n, r) {
super.addEventListener(e, n, r);
addEventListener(t, n, r) {
super.addEventListener(t, n, r);
}

@@ -1512,4 +1632,4 @@ /**

*/
getTask(e) {
return this._tasks.get(e);
getTask(t) {
return this._tasks.get(t);
}

@@ -1521,8 +1641,8 @@ /**

*/
remove(e) {
let n = this.getTask(e);
return n && (this.dispatchEvent(m("remove", n)), this._tasks.delete(e)), this;
remove(t) {
let n = this.getTask(t);
return n && (this.dispatchEvent(l("remove", n)), this._tasks.delete(t)), this;
}
removeEventListener(e, n, r) {
super.removeEventListener(e, n, r);
removeEventListener(t, n, r) {
super.removeEventListener(t, n, r);
}

@@ -1533,5 +1653,5 @@ /**

reset() {
this.dispatchEvent(m("reset"));
for (let e of this._tasks.values())
e.reset();
this.dispatchEvent(l("reset"));
for (let t of this._tasks.values())
t.reset();
}

@@ -1544,13 +1664,24 @@ /**

this.opts.warmup && await this.warmupTasks();
let e = [];
if (this.dispatchEvent(m("start")), this.concurrency === "bench") {
let n = w(this.threshold), r = [];
for (let o of this._tasks.values())
r.push(n(o.run.bind(o)));
e = await Promise.all(r);
let t = [];
if (this.dispatchEvent(l("start")), this.concurrency === "bench") {
let n = k(this.threshold), r = [];
for (let a of this._tasks.values())
r.push(n(a.run.bind(a)));
t = await Promise.all(r);
} else
for (let n of this._tasks.values())
e.push(await n.run());
return this.dispatchEvent(m("complete")), e;
t.push(await n.run());
return this.dispatchEvent(l("complete")), t;
}
runSync() {
m(
this.concurrency === null,
"Cannot use `concurrency` option when using `runSync`"
), this.opts.warmup && this.warmupTasksSync();
let t = [];
this.dispatchEvent(l("start"));
for (let n of this._tasks.values())
t.push(n.runSync());
return this.dispatchEvent(l("complete")), t;
}
/**

@@ -1561,3 +1692,3 @@ * table of the tasks results

*/
table(e) {
table(t) {
return this.tasks.map((n) => {

@@ -1569,7 +1700,7 @@ var r;

Stack: n.result.error.stack
} : (r = e == null ? void 0 : e(n)) != null ? r : {
} : (r = t == null ? void 0 : t(n)) != null ? r : {
"Task name": n.name,
"Latency average (ns)": `${O(n.result.latency.mean).toFixed(2)} \xB1 ${n.result.latency.rme.toFixed(2)}%`,
"Latency average (ns)": `${R(n.result.latency.mean).toFixed(2)} \xB1 ${n.result.latency.rme.toFixed(2)}%`,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
"Latency median (ns)": `${O(n.result.latency.p50).toFixed(2)}${Number.parseFloat(O(n.result.latency.mad).toFixed(2)) > 0 ? ` \xB1 ${O(n.result.latency.mad).toFixed(2)}` : ""}`,
"Latency median (ns)": `${R(n.result.latency.p50).toFixed(2)}${Number.parseFloat(R(n.result.latency.mad).toFixed(2)) > 0 ? ` \xB1 ${R(n.result.latency.mad).toFixed(2)}` : ""}`,
"Throughput average (ops/s)": `${n.result.throughput.mean.toFixed(0)} \xB1 ${n.result.throughput.rme.toFixed(2)}%`,

@@ -1586,18 +1717,26 @@ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion

async warmupTasks() {
if (this.dispatchEvent(m("warmup")), this.concurrency === "bench") {
let e = w(this.threshold), n = [];
if (this.dispatchEvent(l("warmup")), this.concurrency === "bench") {
let t = k(this.threshold), n = [];
for (let r of this._tasks.values())
n.push(e(r.warmup.bind(r)));
n.push(t(r.warmup.bind(r)));
await Promise.all(n);
} else
for (let e of this._tasks.values())
await e.warmup();
for (let t of this._tasks.values())
await t.warmup();
}
/**
* warmup the benchmark tasks (sync version)
*/
warmupTasksSync() {
this.dispatchEvent(l("warmup"));
for (let t of this._tasks.values())
t.warmupSync();
}
};
export {
J as Bench,
F as Task,
S1 as hrtimeNow,
h1 as nToMs,
O as Task,
R1 as hrtimeNow,
a1 as nToMs,
j as now
};
{
"name": "tinybench",
"version": "3.0.7",
"version": "3.1.0",
"type": "module",
"volta": {
"node": "22.11.0",
"pnpm": "9.14.4"
"node": "22.12.0",
"pnpm": "9.15.1"
},

@@ -9,0 +9,0 @@ "engines": {

# Tinybench 🔎
[![CI](https://github.com/tinylibs/tinybench/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/tinylibs/tinybench/actions/workflows/test.yml)
[![NPM version](https://img.shields.io/npm/v/tinybench.svg?style=flat)](https://www.npmjs.com/package/tinybench)
[![CI](https://github.com/tinylibs/tinybench/actions/workflows/qa.yml/badge.svg?branch=main)](https://github.com/tinylibs/tinybench/actions/workflows/qa.yml)
[![NPM version](https://badgen.net/npm/v/tinybench?icon=npm)](https://www.npmjs.com/package/tinybench)
[![Discord](https://badgen.net/discord/online-members/c3UUYNcHrU?icon=discord&label=discord&color=green)](https://discord.gg/c3UUYNcHrU)
[![neostandard Javascript Code Style](<https://badgen.net/static/code style/neostandard/green>)](https://github.com/neostandard/neostandard)

@@ -6,0 +7,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet