Skip to content

Commit 22b54f0

Browse files
refactor: factor out empty function as constant (#148)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
1 parent f2aa36a commit 22b54f0

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export type Hook = (task: Task, mode: 'warmup' | 'run') => void | Promise<void>;
147147
- `afterAll?: () => void | Promise<void>`: invoked once after all iterations of `fn` have finished
148148
- `remove(name: string)`: remove a benchmark task from the task map
149149
- `table()`: table of the tasks results
150-
- `get results(): (TaskResult | undefined)[]`: (getter) tasks results as an array
150+
- `get results(): (Readonly<TaskResult> | undefined)[]`: (getter) tasks results as an array
151151
- `get tasks(): Task[]`: (getter) tasks as an array
152152
- `getTask(name: string): Task | undefined`: get a task based on the name
153153

@@ -163,7 +163,7 @@ function has been executed.
163163
- `fn: Fn`: the task function
164164
- `opts: FnOptions`: Task options
165165
- `runs: number`: the number of times the task function has been executed
166-
- `result?: TaskResult`: the result object
166+
- `result?: Readonly<TaskResult>`: the result object
167167
- `async run()`: run the current task and write the results in `Task.result` object property
168168
- `async warmup()`: warmup the current task
169169
- `reset()`: reset the task to make the `Task.runs` a zero-value and remove the `Task.result` object property

src/bench.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
defaultMinimumTime,
66
defaultMinimumWarmupIterations,
77
defaultMinimumWarmupTime,
8+
emptyFunction,
89
} from './constants';
910
import { createBenchEvent } from './event';
1011
import Task from './task';
@@ -75,10 +76,8 @@ export default class Bench extends EventTarget {
7576
this.iterations = options.iterations ?? this.iterations;
7677
this.signal = options.signal;
7778
this.throws = options.throws ?? this.throws;
78-
// eslint-disable-next-line @typescript-eslint/no-empty-function
79-
this.setup = options.setup ?? (() => {});
80-
// eslint-disable-next-line @typescript-eslint/no-empty-function
81-
this.teardown = options.teardown ?? (() => {});
79+
this.setup = options.setup ?? emptyFunction;
80+
this.teardown = options.teardown ?? emptyFunction;
8281

8382
if (this.signal) {
8483
this.signal.addEventListener(
@@ -236,7 +235,7 @@ export default class Bench extends EventTarget {
236235
/**
237236
* (getter) tasks results as an array
238237
*/
239-
get results(): (TaskResult | undefined)[] {
238+
get results(): (Readonly<TaskResult> | undefined)[] {
240239
return [...this._tasks.values()].map((task) => task.result);
241240
}
242241

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,3 +1036,6 @@ export const defaultMinimumIterations = 10;
10361036
export const defaultMinimumWarmupTime = 100;
10371037

10381038
export const defaultMinimumWarmupIterations = 5;
1039+
1040+
// eslint-disable-next-line @typescript-eslint/no-empty-function
1041+
export const emptyFunction = Object.freeze(() => {});

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { tTable } from './constants';
1+
import { emptyFunction, tTable } from './constants';
22
import type { Fn, Statistics } from './types';
33

44
export const nanoToMs = (nano: number) => nano / 1e6;
@@ -51,8 +51,8 @@ export const isFnAsyncResource = (fn: Fn | undefined | null): boolean => {
5151
if (promiseLike) {
5252
// silence promise rejection
5353
try {
54-
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unnecessary-condition
55-
(fnCall as Promise<unknown>).then(() => {})?.catch(() => {});
54+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
55+
(fnCall as Promise<unknown>).then(emptyFunction)?.catch(emptyFunction);
5656
} catch {
5757
// ignore
5858
}

0 commit comments

Comments
 (0)