Skip to content

Commit

Permalink
refactor!: remove useless benchmark todos (#118)
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit authored Oct 14, 2024
1 parent 46fbd0c commit 434d07f
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 62 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ bench
await new Promise((r) => setTimeout(r, 1)); // we wait 1ms :)
console.log('I am slower');
})
.todo('unimplemented bench');

await bench.warmup(); // make results more reliable, ref: https://github.com/tinylibs/tinybench/pull/50
await bench.run();
Expand All @@ -59,15 +58,6 @@ console.table(bench.table());
// │ 0 │ 'faster task' │ '38,832' │ 25751.297631307978 │ '±3.48%' │ '22016.49999997812±5.5000000145' │ 3884 │
// │ 1 │ 'slower task' │ '669' │ 1493338.567164177 │ '±5.98%' │ '1445076.0000000286' │ 67 │
// └─────────┴───────────────┴──────────┴──────────────────────┴──────────┴──────────────────────────────────┴─────────┘

console.table(bench.table((task) => ({ 'Task name': task.name })));

// Output:
// ┌─────────┬───────────────────────┐
// │ (index) │ Task name │
// ├─────────┼───────────────────────┤
// │ 0 │ 'unimplemented bench' │
// └─────────┴───────────────────────┘
```

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

### `Task`

Expand Down Expand Up @@ -334,7 +322,6 @@ export type BenchEvents =
| 'cycle' // when running each benchmark task gets done (cycle)
| 'add' // when a Task gets added to the Bench
| 'remove' // when a Task gets removed of the Bench
| 'todo'; // when a todo Task gets added to the Bench

/**
* task events
Expand Down
16 changes: 1 addition & 15 deletions examples/src/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ bench
.add('slower task', async () => {
await new Promise((r) => setTimeout(r, 1)); // we wait 1ms :)
console.log('I am slower');
})
.todo('unimplemented bench');
});

await bench.run();

Expand All @@ -24,16 +23,3 @@ console.table(bench.table());
// │ 0 │ 'faster task' │ '38,832' │ 25751.297631307978 │ '±3.48%' │ '22016.49999997812±5.5000000145' │ 3884 │
// │ 1 │ 'slower task' │ '669' │ 1493338.567164177 │ '±5.98%' │ '1445076.0000000286' │ 67 │
// └─────────┴───────────────┴──────────┴──────────────────────┴──────────┴──────────────────────────────────┴─────────┘

console.table(
bench.todos.map(({ name }) => ({
'Task name': name,
})),
);

// Output:
// ┌─────────┬───────────────────────┐
// │ (index) │ Task name │
// ├─────────┼───────────────────────┤
// │ 0 │ 'unimplemented bench' │
// └─────────┴───────────────────────┘
17 changes: 0 additions & 17 deletions src/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export default class Bench extends EventTarget {
*/
private readonly _tasks: Map<string, Task> = new Map();

private readonly _todos: Map<string, Task> = new Map();

/**
* Executes tasks concurrently based on the specified concurrency mode.
*
Expand Down Expand Up @@ -193,17 +191,6 @@ export default class Bench extends EventTarget {
return this;
}

/**
* add a benchmark todo to the todo map
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
todo(name: string, fn: Fn = () => {}, opts: FnOptions = {}) {
const task = new Task(this, name, fn, opts);
this._todos.set(name, task);
this.dispatchEvent(createBenchEvent('todo', task));
return this;
}

/**
* remove a benchmark task from the task map
*/
Expand Down Expand Up @@ -268,10 +255,6 @@ export default class Bench extends EventTarget {
return [...this._tasks.values()];
}

get todos(): Task[] {
return [...this._todos.values()];
}

/**
* get a task based on the task name
*/
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export type BenchEvents =
| 'cycle' // when running each benchmark task gets done (cycle)
| 'add' // when a Task gets added to the Bench
| 'remove' // when a Task gets removed of the Bench
| 'todo'; // when a todo Task gets added to the Bench

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

Expand All @@ -173,7 +172,6 @@ export interface BenchEventsMap{
remove: TaskEventListener
cycle: TaskEventListener
error: TaskEventListener
todo: TaskEventListener
}

/**
Expand Down
15 changes: 0 additions & 15 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,6 @@ test('events order 2', async () => {
await new Promise((resolve) => setTimeout(resolve, 150));
});

test('todo event', async () => {
const bench = new Bench({ time: 50 });

let todoTask: Task;
bench.addEventListener('todo', ({ task }) => {
todoTask = task;
});

bench.todo('unimplemented bench');

await bench.run();

expect(todoTask!.name).toBe('unimplemented bench');
});

test('error event', async () => {
const bench = new Bench({ time: 50 });
const err = new Error();
Expand Down

0 comments on commit 434d07f

Please sign in to comment.