Skip to content

Commit 8fbd137

Browse files
authored
fix: improve types (#423)
1 parent 5a7db22 commit 8fbd137

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

src/bench.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
EventListenerObject,
88
Fn,
99
FnOptions,
10+
JSRuntime,
1011
RemoveEventListenerOptionsArgument,
1112
TaskResult,
1213
} from './types'
@@ -23,7 +24,6 @@ import { Task } from './task'
2324
import {
2425
defaultConvertTaskResultForConsoleTable,
2526
invariant,
26-
type JSRuntime,
2727
performanceNow,
2828
runtime,
2929
runtimeVersion,

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export type {
1010
FnOptions,
1111
FnReturnedObject,
1212
Hook,
13+
JSRuntime,
1314
ResolvedBenchOptions,
1415
Statistics,
1516
TaskEvents,
1617
TaskResult,
1718
} from './types'
18-
export type { JSRuntime } from './utils'
1919
export { hrtimeNow, performanceNow as now, nToMs } from './utils'

src/types.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { BenchEvent } from '../src/event'
22
import type { Task } from '../src/task'
3-
import type { JSRuntime } from './utils'
43
export type { BenchEvent } from '../src/event'
54

65
export type AddEventListenerOptionsArgument = Parameters<
@@ -22,9 +21,9 @@ export type BenchEvents =
2221
| 'warmup' // when the benchmarks start getting warmed up
2322

2423
export type BenchEventsOptionalTask = Omit<BenchEvents, 'add' | 'cycle' | 'error' | 'remove'>
24+
2525
export type BenchEventsWithError = Extract<BenchEvents, 'error'>
2626
export type BenchEventsWithTask = Extract<BenchEvents, 'add' | 'cycle' | 'error' | 'remove'>
27-
2827
export interface BenchLike extends EventTarget {
2928
addEventListener: <K extends BenchEvents>(
3029
type: K,
@@ -144,16 +143,16 @@ export type ConsoleTableConverter = (
144143
*/
145144
export type EventListener<E extends BenchEvents, M extends 'bench' | 'task' = 'bench'> = (evt: BenchEvent<E, M>) => void
146145

146+
export interface EventListenerObject<E extends BenchEvents, M extends 'bench' | 'task' = 'bench'> {
147+
handleEvent(evt: BenchEvent<E, M>): void
148+
}
149+
147150
/**
148151
* Both the `Task` and `Bench` objects extend the `EventTarget` object.
149152
* So you can attach a listeners to different types of events to each class instance
150153
* using the universal `addEventListener` and `removeEventListener` methods.
151154
*/
152155

153-
export interface EventListenerObject<E extends BenchEvents, M extends 'bench' | 'task' = 'bench'> {
154-
handleEvent(evt: BenchEvent<E, M>): void
155-
}
156-
157156
/**
158157
* The task function.
159158
*
@@ -243,6 +242,28 @@ export type Hook = (
243242
mode?: 'run' | 'warmup'
244243
) => Promise<void> | void
245244

245+
/**
246+
* The JavaScript runtime environment.
247+
* @see https://runtime-keys.proposal.wintercg.org/
248+
*/
249+
export type JSRuntime =
250+
| 'browser'
251+
| 'bun'
252+
| 'deno'
253+
| 'edge-light'
254+
| 'fastly'
255+
| 'hermes'
256+
| 'jsc'
257+
| 'lagon'
258+
| 'moddable'
259+
| 'netlify'
260+
| 'node'
261+
| 'quickjs-ng'
262+
| 'spidermonkey'
263+
| 'unknown'
264+
| 'v8'
265+
| 'workerd'
266+
246267
// @types/node doesn't have these types globally, and we don't want to bring "dom" lib for everyone
247268
export type RemoveEventListenerOptionsArgument = Parameters<
248269
typeof EventTarget.prototype.removeEventListener

src/utils.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,12 @@ import type { Task } from './task'
55
import type {
66
ConsoleTableConverter,
77
Fn,
8+
JSRuntime,
89
Statistics,
910
} from './types'
1011

1112
import { emptyFunction, tTable } from './constants'
1213

13-
/**
14-
* The JavaScript runtime environment.
15-
* @see https://runtime-keys.proposal.wintercg.org/
16-
*/
17-
export type JSRuntime =
18-
| 'browser'
19-
| 'bun'
20-
| 'deno'
21-
| 'edge-light'
22-
| 'fastly'
23-
| 'hermes'
24-
| 'jsc'
25-
| 'lagon'
26-
| 'moddable'
27-
| 'netlify'
28-
| 'node'
29-
| 'quickjs-ng'
30-
| 'spidermonkey'
31-
| 'unknown'
32-
| 'v8'
33-
| 'workerd'
34-
3514
/**
3615
* @param g GlobalThis object
3716
* @returns Detected runtime and its version
@@ -310,15 +289,13 @@ export const meanAndVariance = (samples: Samples): { mean: number; vr: number }
310289
}
311290
}
312291

313-
type Quantile = 0.5 | 0.75 | 0.99 | 0.995 | 0.999
314-
315292
/**
316293
* Computes the q-quantile of a sorted sample.
317294
* @param samples - the sorted sample
318295
* @param q - the quantile to compute
319296
* @returns the q-quantile of the sample
320297
*/
321-
const quantileSorted = (samples: SortedSamples, q: Quantile): number => {
298+
const quantileSorted = (samples: SortedSamples, q: 0.5 | 0.75 | 0.99 | 0.995 | 0.999): number => {
322299
const base = (samples.length - 1) * q
323300
const baseIndex = Math.floor(base)
324301

0 commit comments

Comments
 (0)