Skip to content

Commit feae780

Browse files
committed
Internal: refactor arrayToHexString for performance and to avoid branching
1 parent 83f0f45 commit feae780

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lib/utils.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,10 @@ export const hexStringToArray = (hex: string) => {
5454
* @returns Hexadecimal representation of the array
5555
*/
5656
export const arrayToHexString = (bytes: Uint8Array) => {
57-
const res = [];
58-
for (let c = 0; c < bytes.length; c++) {
59-
const hex = bytes[c].toString(16);
60-
res.push(hex.length < 2 ? '0' + hex : hex);
61-
}
62-
return res.join('');
57+
const hexAlphabet = '0123456789abcdef';
58+
let s = '';
59+
bytes.forEach((v) => { s += hexAlphabet[v >> 4] + hexAlphabet[v & 15]; });
60+
return s;
6361
};
6462

6563
/**

0 commit comments

Comments
 (0)