From d25a281513dac04a31762160d862c3844a71b441 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 18 May 2026 10:08:17 -0700 Subject: [PATCH] Eliminate slow modulo operator and unshift function. --- src/lib/convert.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lib/convert.ts b/src/lib/convert.ts index 6362793..801f96e 100644 --- a/src/lib/convert.ts +++ b/src/lib/convert.ts @@ -258,15 +258,14 @@ export class hex { if (typeof padding !== 'number') { throw new TypeError('Invalid padding when converting hex to array', { cause: padding }) } - if (hex.length % 2 === 1) hex = `0${hex}` + if ((hex.length & 1) !== 0) hex = `0${hex}` const hexArray = hex.match(/.{2}/g) if (hexArray == null) { throw new RangeError('Invalid hex string when converting to array', { cause: hexArray }) } - for (let i = hexArray.length; i < padding; i++) { - hexArray.unshift('0') - } - return hexArray.map(v => parseInt(v, 16)) + const diff = padding - hexArray.length + const pad = new Array(diff > 0 ? diff : 0).fill(0) + return pad.concat(hexArray.map(v => parseInt(v, 16))) } /** -- 2.47.3