Skip to content

Commit eedc1a3

Browse files
feat: add name to Bench options (#140)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
1 parent 6692aea commit eedc1a3

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ benchmark tasks to it.
3333
```js
3434
import { Bench } from 'tinybench';
3535

36-
const bench = new Bench({ time: 100 });
36+
const bench = new Bench({ name: 'simple benchmark', time: 100 });
3737

3838
bench
3939
.add('faster task', () => {
@@ -47,9 +47,11 @@ bench
4747
await bench.warmup(); // make results more reliable
4848
await bench.run();
4949

50+
console.log(bench.name);
5051
console.table(bench.table());
5152

5253
// Output:
54+
// simple benchmark
5355
// ┌─────────┬───────────────┬────────────────────────────┬───────────────────────────┬──────────────────────┬─────────────────────┬─────────┐
5456
// │ (index) │ Task name │ Throughput average (ops/s) │ Throughput median (ops/s) │ Latency average (ns) │ Latency median (ns) │ Samples │
5557
// ├─────────┼───────────────┼────────────────────────────┼───────────────────────────┼──────────────────────┼─────────────────────┼─────────┤
@@ -79,6 +81,11 @@ Options:
7981

8082
```ts
8183
export interface Options {
84+
/**
85+
* benchmark name
86+
*/
87+
name?: string;
88+
8289
/**
8390
* time needed for running a benchmark task (milliseconds) @default 500
8491
*/

examples/src/simple.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22
import { Bench } from '../../src';
33

4-
const bench = new Bench({ time: 100 });
4+
const bench = new Bench({ name: 'simple benchmark', time: 100 });
55

66
bench
77
.add('faster task', () => {
@@ -14,9 +14,11 @@ bench
1414

1515
await bench.run();
1616

17+
console.log(bench.name);
1718
console.table(bench.table());
1819

1920
// Output:
21+
// simple benchmark
2022
// ┌─────────┬───────────────┬────────────────────────────┬───────────────────────────┬──────────────────────┬─────────────────────┬─────────┐
2123
// │ (index) │ Task name │ Throughput average (ops/s) │ Throughput median (ops/s) │ Latency average (ns) │ Latency median (ns) │ Samples │
2224
// ├─────────┼───────────────┼────────────────────────────┼───────────────────────────┼──────────────────────┼─────────────────────┼─────────┤

src/bench.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export default class Bench extends EventTarget {
3131
*/
3232
private readonly _tasks = new Map<string, Task>();
3333

34+
name?: string;
35+
3436
/**
3537
* Executes tasks concurrently based on the specified concurrency mode.
3638
*
@@ -65,6 +67,7 @@ export default class Bench extends EventTarget {
6567

6668
constructor(options: Options = {}) {
6769
super();
70+
this.name = options.name;
6871
this.now = options.now ?? this.now;
6972
this.warmupTime = options.warmupTime ?? this.warmupTime;
7073
this.warmupIterations = options.warmupIterations ?? this.warmupIterations;

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ export interface TaskEventsMap {
307307
}
308308

309309
export interface Options {
310+
/**
311+
* benchmark name
312+
*/
313+
name?: string;
314+
310315
/**
311316
* time needed for running a benchmark task (milliseconds) @default 500
312317
*/

0 commit comments

Comments
 (0)