@@ -12,7 +12,7 @@ class Benchmark {
12
12
this . _ended = false ;
13
13
14
14
// Holds process.hrtime value
15
- this . _time = [ 0 , 0 ] ;
15
+ this . _time = 0n ;
16
16
17
17
// Use the file name as the name of the benchmark
18
18
this . name = require . main . filename . slice ( __dirname . length + 1 ) ;
@@ -218,12 +218,12 @@ class Benchmark {
218
218
throw new Error ( 'Called start more than once in a single benchmark' ) ;
219
219
}
220
220
this . _started = true ;
221
- this . _time = process . hrtime ( ) ;
221
+ this . _time = process . hrtime . bigint ( ) ;
222
222
}
223
223
224
224
end ( operations ) {
225
225
// Get elapsed time now and do error checking later for accuracy.
226
- const elapsed = process . hrtime ( this . _time ) ;
226
+ const time = process . hrtime . bigint ( ) ;
227
227
228
228
if ( ! this . _started ) {
229
229
throw new Error ( 'called end without start' ) ;
@@ -237,16 +237,19 @@ class Benchmark {
237
237
if ( ! process . env . NODEJS_BENCHMARK_ZERO_ALLOWED && operations <= 0 ) {
238
238
throw new Error ( 'called end() with operation count <= 0' ) ;
239
239
}
240
- if ( elapsed [ 0 ] === 0 && elapsed [ 1 ] === 0 ) {
240
+
241
+ this . _ended = true ;
242
+
243
+ if ( time === this . _time ) {
241
244
if ( ! process . env . NODEJS_BENCHMARK_ZERO_ALLOWED )
242
245
throw new Error ( 'insufficient clock precision for short benchmark' ) ;
243
246
// Avoid dividing by zero
244
- elapsed [ 1 ] = 1 ;
247
+ this . report ( operations && Number . MAX_VALUE , 0n ) ;
248
+ return ;
245
249
}
246
250
247
- this . _ended = true ;
248
- const time = elapsed [ 0 ] + elapsed [ 1 ] / 1e9 ;
249
- const rate = operations / time ;
251
+ const elapsed = time - this . _time ;
252
+ const rate = operations / ( Number ( elapsed ) / 1e9 ) ;
250
253
this . report ( rate , elapsed ) ;
251
254
}
252
255
@@ -255,12 +258,21 @@ class Benchmark {
255
258
name : this . name ,
256
259
conf : this . config ,
257
260
rate,
258
- time : elapsed [ 0 ] + elapsed [ 1 ] / 1e9 ,
261
+ time : nanoSecondsToString ( elapsed ) ,
259
262
type : 'report' ,
260
263
} ) ;
261
264
}
262
265
}
263
266
267
+ function nanoSecondsToString ( bigint ) {
268
+ const str = bigint . toString ( ) ;
269
+ const decimalPointIndex = str . length - 9 ;
270
+ if ( decimalPointIndex < 0 ) {
271
+ return `0.${ '0' . repeat ( - decimalPointIndex ) } ${ str } ` ;
272
+ }
273
+ return `${ str . slice ( 0 , decimalPointIndex ) } .${ str . slice ( decimalPointIndex ) } ` ;
274
+ }
275
+
264
276
function formatResult ( data ) {
265
277
// Construct configuration string, " A=a, B=b, ..."
266
278
let conf = '' ;
0 commit comments