File tree Expand file tree Collapse file tree 1 file changed +48
-3
lines changed Expand file tree Collapse file tree 1 file changed +48
-3
lines changed Original file line number Diff line number Diff line change @@ -148,11 +148,56 @@ function floatStr(n: number) {
148148 return n . toString ( ) ;
149149}
150150
151+ const round = ( n : number , places : number = 3 ) : string =>
152+ n . toFixed ( places ) . replace ( / \. ? 0 + $ / , "" ) ;
153+
154+ function strDuration ( n : number ) : string {
155+ let unit = "ns" ;
156+ // I could have DRYd this with some defunctionalization
157+ // and a for loop, but I really don't care enough.
158+ if ( n > 1000 ) {
159+ unit = "μs" ;
160+ n /= 1000 ;
161+ }
162+ if ( n > 1000 ) {
163+ unit = "ms" ;
164+ n /= 1000 ;
165+ }
166+ if ( n > 1000 ) {
167+ unit = "s" ;
168+ n /= 1000 ;
169+ }
170+ if ( n > 60 ) {
171+ const mins = Math . floor ( n / 60 ) ;
172+ const secs = n % 60 ;
173+ return `${ mins } m ${ round ( secs ) } s`
174+ }
175+ return `${ round ( n ) } ${ unit } ` ;
176+ }
177+
178+ let NS_RANGE_REGEX = / ^ ± \s * ( [ 0 - 9 ] ) + $ / ;
179+
151180function strVal ( b : BenchmarkResult ) : string {
152- let s = `\`${ b . value } \` ${ b . unit } ` ;
153- if ( b . range ) {
154- s += ` (\`${ b . range } \`)` ;
181+
182+ let s = "" ;
183+ let range = b . range ;
184+
185+ if ( b . unit == 'ns/iter' ) {
186+ s += `\`${ strDuration ( b . value ) } /iter\`` ;
187+ if ( range && range . match ( NS_RANGE_REGEX ) ) {
188+ range = "± " + strDuration ( parseInt ( range . replace ( NS_RANGE_REGEX , "$1" ) ) ) ;
189+ } else {
190+ // If we can't show a unit, just give up...
191+ range = undefined ;
192+ }
193+ } else {
194+ s += `\`${ b . value } ${ b . unit } \`` ;
155195 }
196+
197+ if ( range ) {
198+ s += `<br>(\`${ range } \`)` ;
199+ }
200+
156201 return s ;
157202}
158203
You can’t perform that action at this time.
0 commit comments