@@ -105,15 +105,15 @@ export class Task extends EventTarget {
105105 return this
106106 }
107107 this . dispatchEvent ( createBenchEvent ( 'start' , this ) )
108- await this . bench . opts . setup ?. ( this , 'run' )
108+ await this . bench . opts . setup ( this , 'run' )
109109 const { error, samples : latencySamples } = ( await this . benchmark (
110110 'run' ,
111- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
112- this . bench . opts . time ! ,
113- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
114- this . bench . opts . iterations !
111+
112+ this . bench . opts . time ,
113+
114+ this . bench . opts . iterations
115115 ) ) as { error ?: Error ; samples ?: number [ ] }
116- await this . bench . opts . teardown ?. ( this , 'run' )
116+ await this . bench . opts . teardown ( this , 'run' )
117117
118118 this . processRunResult ( { error, latencySamples } )
119119
@@ -136,21 +136,21 @@ export class Task extends EventTarget {
136136 )
137137 this . dispatchEvent ( createBenchEvent ( 'start' , this ) )
138138
139- const setupResult = this . bench . opts . setup ?. ( this , 'run' )
139+ const setupResult = this . bench . opts . setup ( this , 'run' )
140140 invariant (
141141 ! isPromiseLike ( setupResult ) ,
142142 '`setup` function must be sync when using `runSync()`'
143143 )
144144
145145 const { error, samples : latencySamples } = this . benchmarkSync (
146146 'run' ,
147- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
148- this . bench . opts . time ! ,
149- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
150- this . bench . opts . iterations !
147+
148+ this . bench . opts . time ,
149+
150+ this . bench . opts . iterations
151151 ) as { error ?: Error ; samples ?: number [ ] }
152152
153- const teardownResult = this . bench . opts . teardown ?. ( this , 'run' )
153+ const teardownResult = this . bench . opts . teardown ( this , 'run' )
154154 invariant (
155155 ! isPromiseLike ( teardownResult ) ,
156156 '`teardown` function must be sync when using `runSync()`'
@@ -170,15 +170,15 @@ export class Task extends EventTarget {
170170 return
171171 }
172172 this . dispatchEvent ( createBenchEvent ( 'warmup' , this ) )
173- await this . bench . opts . setup ?. ( this , 'warmup' )
173+ await this . bench . opts . setup ( this , 'warmup' )
174174 const { error } = ( await this . benchmark (
175175 'warmup' ,
176- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
177- this . bench . opts . warmupTime ! ,
178- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
179- this . bench . opts . warmupIterations !
176+
177+ this . bench . opts . warmupTime ,
178+
179+ this . bench . opts . warmupIterations
180180 ) ) as { error ?: Error }
181- await this . bench . opts . teardown ?. ( this , 'warmup' )
181+ await this . bench . opts . teardown ( this , 'warmup' )
182182
183183 this . postWarmup ( error )
184184 }
@@ -194,21 +194,21 @@ export class Task extends EventTarget {
194194
195195 this . dispatchEvent ( createBenchEvent ( 'warmup' , this ) )
196196
197- const setupResult = this . bench . opts . setup ?. ( this , 'warmup' )
197+ const setupResult = this . bench . opts . setup ( this , 'warmup' )
198198 invariant (
199199 ! isPromiseLike ( setupResult ) ,
200200 '`setup` function must be sync when using `runSync()`'
201201 )
202202
203203 const { error } = this . benchmarkSync (
204204 'warmup' ,
205- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
206- this . bench . opts . warmupTime ! ,
207- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
208- this . bench . opts . warmupIterations !
205+
206+ this . bench . opts . warmupTime ,
207+
208+ this . bench . opts . warmupIterations
209209 ) as { error ?: Error }
210210
211- const teardownResult = this . bench . opts . teardown ?. ( this , 'warmup' )
211+ const teardownResult = this . bench . opts . teardown ( this , 'warmup' )
212212 invariant (
213213 ! isPromiseLike ( teardownResult ) ,
214214 '`teardown` function must be sync when using `runSync()`'
@@ -240,19 +240,25 @@ export class Task extends EventTarget {
240240
241241 let taskTime = 0 // ms;
242242 if ( this . async ) {
243- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
244- const taskStart = this . bench . opts . now ! ( )
243+ const taskStart = this . bench . opts . now ( )
245244 // eslint-disable-next-line no-useless-call
246- await this . fn . call ( this )
247- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
248- taskTime = this . bench . opts . now ! ( ) - taskStart
245+ const fnResult = await this . fn . call ( this )
246+ taskTime = this . bench . opts . now ( ) - taskStart
247+
248+ const overriddenDuration = getOverriddenDurationFromFnResult ( fnResult )
249+ if ( overriddenDuration != null ) {
250+ taskTime = overriddenDuration
251+ }
249252 } else {
250- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
251- const taskStart = this . bench . opts . now ! ( )
253+ const taskStart = this . bench . opts . now ( )
252254 // eslint-disable-next-line no-useless-call
253- this . fn . call ( this )
254- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
255- taskTime = this . bench . opts . now ! ( ) - taskStart
255+ const fnResult = this . fn . call ( this )
256+ taskTime = this . bench . opts . now ( ) - taskStart
257+
258+ const overriddenDuration = getOverriddenDurationFromFnResult ( fnResult )
259+ if ( overriddenDuration != null ) {
260+ taskTime = overriddenDuration
261+ }
256262 }
257263
258264 samples . push ( taskTime )
@@ -326,18 +332,21 @@ export class Task extends EventTarget {
326332
327333 let taskTime = 0 // ms;
328334
329- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
330- const taskStart = this . bench . opts . now ! ( )
335+ const taskStart = this . bench . opts . now ( )
331336 // eslint-disable-next-line no-useless-call
332- const result = this . fn . call ( this )
333- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
334- taskTime = this . bench . opts . now ! ( ) - taskStart
337+ const fnResult = this . fn . call ( this )
338+ taskTime = this . bench . opts . now ( ) - taskStart
335339
336340 invariant (
337- ! isPromiseLike ( result ) ,
341+ ! isPromiseLike ( fnResult ) ,
338342 'task function must be sync when using `runSync()`'
339343 )
340344
345+ const overriddenDuration = getOverriddenDurationFromFnResult ( fnResult )
346+ if ( overriddenDuration != null ) {
347+ taskTime = overriddenDuration
348+ }
349+
341350 samples . push ( taskTime )
342351 totalTime += taskTime
343352
@@ -467,3 +476,14 @@ export class Task extends EventTarget {
467476 this . dispatchEvent ( createBenchEvent ( 'complete' , this ) )
468477 }
469478}
479+
480+ /**
481+ *
482+ * @param fnResult - the result of the task function.
483+ * @returns the overridden duration if defined by the function.
484+ */
485+ function getOverriddenDurationFromFnResult ( fnResult : ReturnType < Fn > ) : number | undefined {
486+ if ( fnResult != null && typeof fnResult === 'object' && 'overriddenDuration' in fnResult && typeof fnResult . overriddenDuration === 'number' ) {
487+ return fnResult . overriddenDuration
488+ }
489+ }
0 commit comments