@@ -15,11 +15,11 @@ declare const global: {
15
15
const kNanosecondsPerSeconds = 1e9 ;
16
16
const kBenchmarkIterationMaxCount = 10000 ;
17
17
const kBenchmarkTimeoutInMsec = 5000 ;
18
- const kWarmupIterationCount = 10 ;
18
+ const kWarmupIterationCount = 100 ;
19
19
const kTopMetricCount = 5 ;
20
20
21
21
22
- function _run ( fn : ( ) => void , collector : number [ ] ) {
22
+ function _run ( fn : ( i : number ) => void , collector : number [ ] ) {
23
23
const timeout = Date . now ( ) ;
24
24
// Gather the first 5 seconds runs, or kMaxNumberOfIterations runs whichever comes first
25
25
// (soft timeout).
@@ -28,7 +28,7 @@ function _run(fn: () => void, collector: number[]) {
28
28
i ++ ) {
29
29
// Start time.
30
30
const start = process . hrtime ( ) ;
31
- fn ( ) ;
31
+ fn ( i ) ;
32
32
// Get the stop difference time.
33
33
const diff = process . hrtime ( start ) ;
34
34
@@ -41,13 +41,15 @@ function _run(fn: () => void, collector: number[]) {
41
41
function _stats ( metrics : number [ ] ) {
42
42
metrics . sort ( ( a , b ) => a - b ) ;
43
43
44
- const middle = metrics . length / 2 ;
44
+ const count = metrics . length ;
45
+ const middle = count / 2 ;
45
46
const mean = Number . isInteger ( middle )
46
47
? metrics [ middle ] : ( ( metrics [ middle - 0.5 ] + metrics [ middle + 0.5 ] ) / 2 ) ;
47
48
const total = metrics . reduce ( ( acc , curr ) => acc + curr , 0 ) ;
48
- const average = total / metrics . length ;
49
+ const average = total / count ;
49
50
50
51
return {
52
+ count : count ,
51
53
fastest : metrics . slice ( 0 , kTopMetricCount ) ,
52
54
slowest : metrics . reverse ( ) . slice ( 0 , kTopMetricCount ) ,
53
55
mean,
@@ -56,12 +58,12 @@ function _stats(metrics: number[]) {
56
58
}
57
59
58
60
59
- export function benchmark ( name : string , fn : ( ) => void , base ?: ( ) => void ) {
61
+ export function benchmark ( name : string , fn : ( i : number ) => void , base ?: ( i : number ) => void ) {
60
62
it ( name + ' (time in nanoseconds)' , ( done ) => {
61
63
process . nextTick ( ( ) => {
62
64
for ( let i = 0 ; i < kWarmupIterationCount ; i ++ ) {
63
65
// Warm it up.
64
- fn ( ) ;
66
+ fn ( i ) ;
65
67
}
66
68
67
69
const reporter = global . benchmarkReporter ;
0 commit comments