From: Chris Duncan Date: Mon, 18 May 2026 17:08:17 +0000 (-0700) Subject: Eliminate slow modulo operator and unshift function. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=d25a281513dac04a31762160d862c3844a71b441;p=libnemo.git Eliminate slow modulo operator and unshift function. --- 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))) } /**