@@ -15,11 +15,11 @@ declare const global: {
1515const kNanosecondsPerSeconds = 1e9 ;
1616const kBenchmarkIterationMaxCount = 10000 ;
1717const kBenchmarkTimeoutInMsec = 5000 ;
18- const kWarmupIterationCount = 10 ;
18+ const kWarmupIterationCount = 100 ;
1919const kTopMetricCount = 5 ;
2020
2121
22- function _run ( fn : ( ) => void , collector : number [ ] ) {
22+ function _run ( fn : ( i : number ) => void , collector : number [ ] ) {
2323 const timeout = Date . now ( ) ;
2424 // Gather the first 5 seconds runs, or kMaxNumberOfIterations runs whichever comes first
2525 // (soft timeout).
@@ -28,7 +28,7 @@ function _run(fn: () => void, collector: number[]) {
2828 i ++ ) {
2929 // Start time.
3030 const start = process . hrtime ( ) ;
31- fn ( ) ;
31+ fn ( i ) ;
3232 // Get the stop difference time.
3333 const diff = process . hrtime ( start ) ;
3434
@@ -41,13 +41,15 @@ function _run(fn: () => void, collector: number[]) {
4141function _stats ( metrics : number [ ] ) {
4242 metrics . sort ( ( a , b ) => a - b ) ;
4343
44- const middle = metrics . length / 2 ;
44+ const count = metrics . length ;
45+ const middle = count / 2 ;
4546 const mean = Number . isInteger ( middle )
4647 ? metrics [ middle ] : ( ( metrics [ middle - 0.5 ] + metrics [ middle + 0.5 ] ) / 2 ) ;
4748 const total = metrics . reduce ( ( acc , curr ) => acc + curr , 0 ) ;
48- const average = total / metrics . length ;
49+ const average = total / count ;
4950
5051 return {
52+ count : count ,
5153 fastest : metrics . slice ( 0 , kTopMetricCount ) ,
5254 slowest : metrics . reverse ( ) . slice ( 0 , kTopMetricCount ) ,
5355 mean,
@@ -56,12 +58,12 @@ function _stats(metrics: number[]) {
5658}
5759
5860
59- export function benchmark ( name : string , fn : ( ) => void , base ?: ( ) => void ) {
61+ export function benchmark ( name : string , fn : ( i : number ) => void , base ?: ( i : number ) => void ) {
6062 it ( name + ' (time in nanoseconds)' , ( done ) => {
6163 process . nextTick ( ( ) => {
6264 for ( let i = 0 ; i < kWarmupIterationCount ; i ++ ) {
6365 // Warm it up.
64- fn ( ) ;
66+ fn ( i ) ;
6567 }
6668
6769 const reporter = global . benchmarkReporter ;
0 commit comments