()
| 148 | class BenchmarkJS extends Metabench { |
| 149 | runner = "benchmarkjs"; |
| 150 | async run() { |
| 151 | const suite = new Benchmark.Suite(); |
| 152 | console.log(` benchmarking ${chalk.white(this.name)} with ${this.runner}`); |
| 153 | for (const name in this.benchmarks) { |
| 154 | const fn = this.benchmarks[name]; |
| 155 | suite.add(name, fn); |
| 156 | } |
| 157 | suite.on("cycle", (event: Benchmark.Event) => { |
| 158 | // const target = event.target; |
| 159 | // console.log(target.name, target.hz, target.stats!.mean); |
| 160 | console.log(chalk.white.dim(` → ${String(event.target)}`)); |
| 161 | // print summary |
| 162 | }); |
| 163 | suite.on("complete", (event: Benchmark.Event) => { |
| 164 | // print summary |
| 165 | const suite = event.currentTarget as Benchmark.Suite; |
| 166 | |
| 167 | // for(const benchmark of suite){} |
| 168 | const results: { |
| 169 | name: string; |
| 170 | hz: number; |
| 171 | mean: number; |
| 172 | rme: number; |
| 173 | samples: number; |
| 174 | }[] = suite.map((benchmark: Benchmark) => ({ |
| 175 | name: benchmark.name, |
| 176 | hz: benchmark.hz, |
| 177 | mean: benchmark.stats.mean, |
| 178 | rme: benchmark.stats.rme, |
| 179 | samples: benchmark.stats.sample.length, |
| 180 | })); |
| 181 | |
| 182 | results.sort((a, b) => a.hz - b.hz).reverse(); |
| 183 | const slowest = results[results.length - 1]; |
| 184 | |
| 185 | const table = new Table({ |
| 186 | columns: [ |
| 187 | { name: "name", color: "white" }, |
| 188 | { name: "summary", alignment: "left" }, |
| 189 | { name: "ops/sec", color: "cyan" }, |
| 190 | { name: "time/op", color: "magenta" }, |
| 191 | { name: "margin", color: "magenta" }, |
| 192 | { name: "samples", color: "magenta" }, |
| 193 | ], |
| 194 | }); |
| 195 | for (const result of results) { |
| 196 | table.addRow({ |
| 197 | name: result.name, |
| 198 | summary: |
| 199 | result === slowest ? "slowest" : `${(result.hz / slowest.hz).toFixed(3)}x faster than ${slowest.name}`, |
| 200 | "ops/sec": `${formatNumber(result.hz)} ops/sec`, |
| 201 | "time/op": `${formatNumber(1 / result.hz)}s`, |
| 202 | margin: `±${result.rme.toFixed(2)}%`, |
| 203 | samples: result.samples, |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | const rendered = ` ${table.render().split("\n").join("\n ")}`; |
nothing calls this directly
no test coverage detected