@@ -90,7 +90,7 @@ export class Measurement {
9090/**
9191 * Options for Benchmark.measure().
9292 */
93- export interface MeasureOptions {
93+ export interface MeasureOptions < BeforeEachReturnType = any , FunctionReturnType = any > {
9494 /**
9595 * The number of times to call the function and measure its duration.
9696 * @default 100
@@ -136,12 +136,12 @@ export interface MeasureOptions {
136136 /**
137137 * Callback to invoke before each iteration.
138138 */
139- beforeEach ?: ( ) => any ;
139+ beforeEach ?: ( ) => BeforeEachReturnType ;
140140
141141 /**
142142 * Callback to invoke after each iteration.
143143 */
144- afterEach ?: ( ) => any ;
144+ afterEach ?: ( beforeEachValue : Awaited < BeforeEachReturnType > , functionValue : Awaited < FunctionReturnType > ) => any ;
145145
146146 /**
147147 * Whether to make use of the options like `meanUnder` and `minUnder`.
@@ -186,12 +186,6 @@ export interface BenchmarkData {
186186 } ;
187187}
188188
189- async function maybePromise ( fn : ( ) => any ) : Promise < void > {
190- const ret = fn ( ) ;
191- if ( ret instanceof Promise ) {
192- await ret ;
193- }
194- }
195189
196190function round ( value : number , places : number = 5 ) : number {
197191 return mathjs . round ( value , places ) as number ;
@@ -203,24 +197,28 @@ function round(value: number, places: number = 5): number {
203197 * @param fn - Function to measure.
204198 * @param options - Options to customize the measurement.
205199 */
206- export async function measure ( fn : ( ) => any , options : Partial < MeasureOptions > = { } ) : Promise < Measurement > {
200+ export async function measure < BeforeEachReturnType = any , FunctionReturnType = any > (
201+ fn : ( beforeEachValue : Awaited < BeforeEachReturnType > ) => FunctionReturnType ,
202+ options : Partial < MeasureOptions < BeforeEachReturnType , FunctionReturnType > > = { }
203+ ) : Promise < Measurement > {
207204 const mergedOptions = { ...defaultMeasureOptions , ...options } ;
208205 const durations : Array < number > = [ ] ;
209206 let calls : Array < Function > = [ ] ;
210207
211208 for ( let i = 0 ; i < mergedOptions . iterations ; i ++ ) {
212209 calls . push ( async ( ) => {
210+ let beforeEachValue : BeforeEachReturnType | undefined = undefined ;
213211 if ( mergedOptions . beforeEach !== undefined ) {
214- await maybePromise ( mergedOptions . beforeEach ) ;
212+ beforeEachValue = await mergedOptions . beforeEach ( ) ;
215213 }
216214
217215 const startTime = hrtime ( ) ;
218- await maybePromise ( fn ) ;
216+ const functionValue = await fn ( beforeEachValue as Awaited < BeforeEachReturnType > ) ;
219217 const [ durationSec , durationNano ] = hrtime ( startTime ) ;
220218 durations . push ( durationSec * 1e3 + durationNano / 1e6 ) ;
221219
222220 if ( mergedOptions . afterEach !== undefined ) {
223- await maybePromise ( mergedOptions . afterEach ) ;
221+ await mergedOptions . afterEach ( beforeEachValue as Awaited < BeforeEachReturnType > , functionValue as Awaited < FunctionReturnType > ) ;
224222 }
225223 } ) ;
226224 }
0 commit comments