()
| 80 | class Tinybench extends Metabench { |
| 81 | runner = "tinybench"; |
| 82 | async run() { |
| 83 | const bench = new Bench({ time: 1500 }); |
| 84 | for (const [name, fn] of Object.entries(this.benchmarks)) { |
| 85 | bench.add(name, fn as any); |
| 86 | } |
| 87 | // await runBench(this.name, bench); |
| 88 | console.log(); |
| 89 | console.log(` benchmarking ${chalk.bold.white(this.name)} with ${chalk.bold.white(this.runner)}`); |
| 90 | |
| 91 | bench.addEventListener("cycle", (e) => { |
| 92 | const task = e.task?.result; |
| 93 | if (!task) throw new Error("Task has no result"); |
| 94 | |
| 95 | console.log( |
| 96 | chalk.dim(" ") + |
| 97 | chalk.white.dim(`→ `) + |
| 98 | chalk.white(e.task.name) + |
| 99 | chalk.white.dim(" ") + |
| 100 | chalk.cyan(formatNumber(task.hz)) + |
| 101 | chalk.cyan(` ops/sec`) + |
| 102 | chalk.dim(` (${e.task.result?.totalTime.toFixed(2)}ms)`) |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | await bench.warmup(); |
| 107 | await bench.run(); |
| 108 | |
| 109 | const sorted = bench.tasks.sort((a, b) => { |
| 110 | if (!a.result || !b.result) throw new Error("Task has no result"); |
| 111 | return a.result?.mean - b.result?.mean; |
| 112 | }); |
| 113 | // const fastest = sorted[0]; |
| 114 | const slowest = sorted[sorted.length - 1]; |
| 115 | |
| 116 | const table = new Table({ |
| 117 | columns: [ |
| 118 | { name: "name", color: "white" }, |
| 119 | { name: "summary", alignment: "left" }, |
| 120 | { name: "ops/sec", color: "cyan" }, |
| 121 | { name: "time/op", color: "magenta" }, |
| 122 | { name: "margin", color: "magenta" }, |
| 123 | { name: "samples", color: "magenta" }, |
| 124 | ], |
| 125 | }); |
| 126 | |
| 127 | for (const task of sorted) { |
| 128 | const result = task.result; |
| 129 | if (!result) throw new Error("Task has no result"); |
| 130 | if (!slowest.result) throw new Error("Task has no result"); |
| 131 | table.addRow({ |
| 132 | name: task.name, |
| 133 | summary: |
| 134 | task === slowest ? "slowest" : `${(result.hz / slowest.result.hz).toFixed(3)}x faster than ${slowest.name}`, |
| 135 | "ops/sec": `${formatNumber(result.hz)} ops/sec`, |
| 136 | "time/op": `${formatNumber(result.mean / 1000)}s`, |
| 137 | margin: `±${result.rme.toFixed(2)}%`, |
| 138 | samples: result.samples.length, |
| 139 | }); |
nothing calls this directly
no test coverage detected