@@ -9,7 +9,7 @@ const padStr = (str, max) => {
99 return str + repeatStr ( ' ' , length )
1010}
1111
12- const reporter = ( results ) => {
12+ const reporter = ( results , compare = [ ] ) => {
1313 const reports = [ {
1414 name : 'Benchmark Name' ,
1515 ops : 'Ops / ms' ,
@@ -22,34 +22,51 @@ const reporter = (results) => {
2222 let maxMemWidth = reports [ 0 ] . mem . length
2323
2424 for ( const benchmark of results ) {
25+ const savedBenchmark = compare . find ( c => c . name === benchmark . name )
26+
2527 const nameWidth = benchmark . name . length
2628 if ( maxNameWidth < nameWidth ) {
2729 maxNameWidth = nameWidth
2830 }
2931
30- const totalMs = ( benchmark . elapsed / 1000000 ) . toFixed ( 4 )
31- const opsPerMs = ( benchmark . stats . count / totalMs ) . toFixed ( 4 )
32- const memUsed = benchmark . memory . after . heapUsed - benchmark . memory . before . heapUsed
33- const memUsedMb = ( memUsed / 1024 / 1024 ) . toFixed ( 2 )
32+ const getCalculations = ( b ) => {
33+ const totalMs = ( b . elapsed / 1000000 ) . toFixed ( 4 )
34+ const opsPerMs = ( b . stats . count / totalMs ) . toFixed ( 4 )
35+ const memUsed = b . memory . after . heapUsed - b . memory . before . heapUsed
36+ const memUsedMb = ( memUsed / 1024 / 1024 ) . toFixed ( 2 )
37+ return { totalMs, opsPerMs, memUsed, memUsedMb }
38+ }
39+
40+ const calculations = getCalculations ( benchmark )
41+ const savedCalculations = savedBenchmark ? getCalculations ( savedBenchmark ) : undefined
42+ const getOutput = ( type ) => {
43+ const delta = savedCalculations ? ( calculations [ type ] - savedCalculations [ type ] ) . toFixed ( 2 ) : 0
44+ return delta ? `${ calculations [ type ] } (${ delta } )` : calculations [ type ]
45+ }
3446
35- const opsWidth = opsPerMs . toString ( ) . length
47+ const ops = getOutput ( 'opsPerMs' )
48+ const opsWidth = ops . toString ( ) . length
3649 if ( maxOpsWidth < opsWidth ) {
3750 maxOpsWidth = opsWidth
3851 }
52+
53+ const totalMs = getOutput ( 'totalMs' )
3954 const totalWidth = totalMs . toString ( ) . length
4055 if ( maxTotalWidth < totalWidth ) {
4156 maxTotalWidth = totalWidth
4257 }
43- const memWidth = memUsedMb . toString ( ) . length
58+
59+ const mem = getOutput ( 'memUsedMb' )
60+ const memWidth = mem . toString ( ) . length
4461 if ( maxMemWidth < memWidth ) {
4562 maxMemWidth = memWidth
4663 }
4764
4865 reports . push ( {
4966 name : benchmark . name ,
50- ops : opsPerMs ,
51- totalMs : totalMs ,
52- mem : memUsedMb
67+ ops,
68+ totalMs,
69+ mem
5370 } )
5471 }
5572
0 commit comments