@@ -102,9 +102,10 @@ async function collectSamples(modulePath) {
102
102
// If time permits, increase sample size to reduce the margin of error.
103
103
const start = Date . now ( ) ;
104
104
while ( samples . length < minSamples || ( Date . now ( ) - start ) / 1e3 < maxTime ) {
105
- const { clocked } = await sampleModule ( modulePath ) ;
105
+ const { clocked, memUsed } = await sampleModule ( modulePath ) ;
106
106
assert ( clocked > 0 ) ;
107
- samples . push ( clocked ) ;
107
+ assert ( memUsed > 0 ) ;
108
+ samples . push ( { clocked, memUsed } ) ;
108
109
}
109
110
return samples ;
110
111
}
@@ -126,15 +127,18 @@ function computeStats(samples) {
126
127
127
128
// Compute the sample mean (estimate of the population mean).
128
129
let mean = 0 ;
129
- for ( const x of samples ) {
130
- mean += x ;
130
+ let meanMemUsed = 0 ;
131
+ for ( const { clocked, memUsed } of samples ) {
132
+ mean += clocked ;
133
+ meanMemUsed += memUsed ;
131
134
}
132
135
mean /= samples . length ;
136
+ meanMemUsed /= samples . length ;
133
137
134
138
// Compute the sample variance (estimate of the population variance).
135
139
let variance = 0 ;
136
- for ( const x of samples ) {
137
- variance += Math . pow ( x - mean , 2 ) ;
140
+ for ( const { clocked } of samples ) {
141
+ variance += Math . pow ( clocked - mean , 2 ) ;
138
142
}
139
143
variance /= samples . length - 1 ;
140
144
@@ -157,22 +161,28 @@ function computeStats(samples) {
157
161
const rme = ( moe / mean ) * 100 || 0 ;
158
162
159
163
return {
164
+ memPerOp : Math . floor ( meanMemUsed ) ,
160
165
ops : NS_PER_SEC / mean ,
161
166
deviation : rme ,
167
+ numSamples : samples . length ,
162
168
} ;
163
169
}
164
170
165
171
function beautifyBenchmark ( results ) {
166
172
const nameMaxLen = maxBy ( results , ( { name } ) => name . length ) ;
167
173
const opsTop = maxBy ( results , ( { ops } ) => ops ) ;
168
174
const opsMaxLen = maxBy ( results , ( { ops } ) => beautifyNumber ( ops ) . length ) ;
175
+ const memPerOpMaxLen = maxBy (
176
+ results ,
177
+ ( { memPerOp } ) => beautifyBytes ( memPerOp ) . length ,
178
+ ) ;
169
179
170
180
for ( const result of results ) {
171
181
printBench ( result ) ;
172
182
}
173
183
174
184
function printBench ( bench ) {
175
- const { name, ops, deviation, samples } = bench ;
185
+ const { name, memPerOp , ops, deviation, numSamples } = bench ;
176
186
console . log (
177
187
' ' +
178
188
nameStr ( ) +
@@ -182,7 +192,10 @@ function beautifyBenchmark(results) {
182
192
grey ( '\xb1' ) +
183
193
deviationStr ( ) +
184
194
cyan ( '%' ) +
185
- grey ( ' (' + samples . length + ' runs sampled)' ) ,
195
+ grey ( ' x ' ) +
196
+ memPerOpStr ( ) +
197
+ '/op' +
198
+ grey ( ' (' + numSamples + ' runs sampled)' ) ,
186
199
) ;
187
200
188
201
function nameStr ( ) {
@@ -200,9 +213,19 @@ function beautifyBenchmark(results) {
200
213
const colorFn = deviation > 5 ? red : deviation > 2 ? yellow : green ;
201
214
return colorFn ( deviation . toFixed ( 2 ) ) ;
202
215
}
216
+
217
+ function memPerOpStr ( ) {
218
+ return beautifyBytes ( memPerOp ) . padStart ( memPerOpMaxLen ) ;
219
+ }
203
220
}
204
221
}
205
222
223
+ function beautifyBytes ( bytes ) {
224
+ const sizes = [ 'Bytes' , 'KB' , 'MB' , 'GB' ] ;
225
+ const i = Math . floor ( Math . log2 ( bytes ) / 10 ) ;
226
+ return beautifyNumber ( bytes / Math . pow ( 2 , i * 10 ) ) + ' ' + sizes [ i ] ;
227
+ }
228
+
206
229
function beautifyNumber ( num ) {
207
230
return Number ( num . toFixed ( num > 100 ? 0 : 2 ) ) . toLocaleString ( ) ;
208
231
}
0 commit comments