File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -115,10 +115,19 @@ export function byteLength(input: string | Buffer | MockttpSerializedBuffer | Ui
115
115
}
116
116
}
117
117
118
+ // We sometimes do hex encoding of large buffers, so we need it to be quick! We handle that by
119
+ // precalculating hex pairs, so we can do it in a simple for loop + join. This is nearly 5x
120
+ // faster than just toString('hex') (before thinking about splitting & spaces) or map(toString(16)).
121
+ const HEX_LOOKUP = Array . from ( { length : 256 } , ( _ , i ) =>
122
+ i . toString ( 16 ) . padStart ( 2 , '0' ) . toUpperCase ( )
123
+ ) ;
124
+
118
125
export function bufferToHex ( input : Buffer ) {
119
- return input . toString ( 'hex' )
120
- . replace ( / ( \w \w ) / g, '$1 ' )
121
- . trimRight ( ) ;
126
+ const hexPairs = new Array ( input . length ) ;
127
+ for ( let i = 0 ; i < input . length ; i ++ ) {
128
+ hexPairs [ i ] = HEX_LOOKUP [ input [ i ] ] ;
129
+ }
130
+ return hexPairs . join ( ' ' ) ;
122
131
}
123
132
124
133
export function getReadableSize ( input : number | Buffer | string , siUnits = true ) {
You can’t perform that action at this time.
0 commit comments