Skip to content

Commit 76461a7

Browse files
authored
feat: add bench.todo (#39)
* feat: add bench.todo * docs: update README.md
1 parent 3543937 commit 76461a7

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ bench
3939
await new Promise(r => setTimeout(r, 1)) // we wait 1ms :)
4040
console.log('I am slower')
4141
})
42+
.todo('unimplemented bench')
4243

4344
await bench.run();
4445

@@ -51,6 +52,19 @@ console.table(bench.table());
5152
// │ 0 │ 'faster task' │ '41,621' │ 24025.791819761525 │ '±20.50%' │ 4257 │
5253
// │ 1 │ 'slower task' │ '828' │ 1207382.7838323202 │ '±7.07%' │ 83 │
5354
// └─────────┴───────────────┴──────────┴────────────────────┴───────────┴─────────┘
55+
56+
console.table(
57+
bench.todos.map(({ name }) => ({
58+
'Task name': name,
59+
})),
60+
);
61+
62+
// Output:
63+
// ┌─────────┬───────────────────────┐
64+
// │ (index) │ Task name │
65+
// ├─────────┼───────────────────────┤
66+
// │ 0 │ 'unimplemented bench' │
67+
// └─────────┴───────────────────────┘
5468
```
5569

5670
The `add` method accepts a task name and a task function, so it can benchmark
@@ -133,6 +147,8 @@ export type Hook = (task: Task, mode: "warmup" | "run") => void | Promise<void>;
133147
- `get results(): (TaskResult | undefined)[]`: (getter) tasks results as an array
134148
- `get tasks(): Task[]`: (getter) tasks as an array
135149
- `getTask(name: string): Task | undefined`: get a task based on the name
150+
- `todo(name: string, fn?: Fn, opts: FnOptions)`: add a benchmark todo to the todo map
151+
- `get todos(): Task[]`: (getter) tasks todos as an array
136152

137153
### `Task`
138154

@@ -298,7 +314,8 @@ export type BenchEvents =
298314
| "warmup" // when the benchmarks start getting warmed up (before start)
299315
| "cycle" // when running each benchmark task gets done (cycle)
300316
| "add" // when a Task gets added to the Bench
301-
| "remove"; // when a Task gets removed of the Bench
317+
| "remove" // when a Task gets removed of the Bench
318+
| "todo"; // when a todo Task gets added to the Bench
302319

303320
/**
304321
* task events

examples/src/simple.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ bench
99
.add('slower task', async () => {
1010
await new Promise((r) => setTimeout(r, 1)); // we wait 1ms :)
1111
console.log('I am slower');
12-
});
12+
})
13+
.todo('unimplemented bench');
1314

1415
await bench.run();
1516

@@ -22,3 +23,16 @@ console.table(bench.table());
2223
// │ 0 │ 'faster task' │ '41,621' │ 24025.791819761525 │ '±20.50%' │ 4257 │
2324
// │ 1 │ 'slower task' │ '828' │ 1207382.7838323202 │ '±7.07%' │ 83 │
2425
// └─────────┴───────────────┴──────────┴────────────────────┴───────────┴─────────┘
26+
27+
console.table(
28+
bench.todos.map(({ name }) => ({
29+
'Task name': name,
30+
})),
31+
);
32+
33+
// Output:
34+
// ┌─────────┬───────────────────────┐
35+
// │ (index) │ Task name │
36+
// ├─────────┼───────────────────────┤
37+
// │ 0 │ 'unimplemented bench' │
38+
// └─────────┴───────────────────────┘

src/bench.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export default class Bench extends EventTarget {
2121
*/
2222
_tasks: Map<string, Task> = new Map();
2323

24+
_todos: Map<string, Task> = new Map();
25+
2426
signal?: AbortSignal;
2527

2628
warmupTime = 100;
@@ -108,6 +110,17 @@ export default class Bench extends EventTarget {
108110
return this;
109111
}
110112

113+
/**
114+
* add a benchmark todo to the todo map
115+
*/
116+
// eslint-disable-next-line @typescript-eslint/no-empty-function
117+
todo(name: string, fn: Fn = () => {}, opts: FnOptions = {}) {
118+
const task = new Task(this, name, fn, opts);
119+
this._todos.set(name, task);
120+
this.dispatchEvent(createBenchEvent('todo', task));
121+
return this;
122+
}
123+
111124
/**
112125
* remove a benchmark task from the task map
113126
*/
@@ -166,6 +179,10 @@ export default class Bench extends EventTarget {
166179
return [...this._tasks.values()];
167180
}
168181

182+
get todos(): Task[] {
183+
return [...this._todos.values()];
184+
}
185+
169186
/**
170187
* get a task based on the task name
171188
*/

test/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ test('events order 2', async () => {
187187
await new Promise((resolve) => setTimeout(resolve, 150));
188188
});
189189

190+
test('todo event', async () => {
191+
const bench = new Bench({ time: 50 });
192+
193+
let todoTask: Task;
194+
bench.addEventListener('todo', ({ task }) => {
195+
todoTask = task;
196+
});
197+
198+
bench.todo('unimplemented bench');
199+
200+
await bench.run();
201+
202+
expect(todoTask!.name).toBe('unimplemented bench');
203+
});
204+
190205
test('error event', async () => {
191206
const bench = new Bench({ time: 50 });
192207
const err = new Error();

types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ export type BenchEvents =
145145
| 'warmup' // when the benchmarks start getting warmed up (before start)
146146
| 'cycle' // when running each benchmark task gets done (cycle)
147147
| 'add' // when a Task gets added to the Bench
148-
| 'remove'; // when a Task gets removed of the Bench
148+
| 'remove' // when a Task gets removed of the Bench
149+
| 'todo'; // when a todo Task gets added to the Bench
149150

150151
export type Hook = (task: Task, mode: 'warmup' | 'run') => void | Promise<void>;
151152

@@ -162,6 +163,7 @@ export interface BenchEventsMap{
162163
remove: TaskEventListener
163164
cycle: TaskEventListener
164165
error: TaskEventListener
166+
todo: TaskEventListener
165167
}
166168

167169
/**

0 commit comments

Comments
 (0)