Skip to content

Commit 6e71db0

Browse files
committed
perf: restore Node.js fromHex performance on very small arrays
1 parent f96a152 commit 6e71db0

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

hex.node.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export const fromHex = Uint8Array.fromHex
2222
if (typeof str !== 'string') throw new TypeError(E_STRING)
2323
if (str.length % 2 !== 0) throw new SyntaxError(E_HEX)
2424
if (denoBug && /[^\dA-Fa-f]/.test(str)) throw new SyntaxError(E_HEX)
25+
26+
if (str.length <= 128) {
27+
const u8 = new Uint8Array(Buffer.from(str, 'hex')) // just copy to not access .buffer
28+
if (u8.length * 2 !== str.length) throw new SyntaxError(E_HEX)
29+
return typedView(u8, format)
30+
}
31+
2532
const length = str.length / 2
2633
const u8 = new Uint8Array(length)
2734
const count = Buffer.from(u8.buffer, u8.byteOffset, u8.byteLength).hexWrite(str, 0, length)

0 commit comments

Comments
 (0)