Skip to content

Commit b9cb350

Browse files
committed
perf(bits): reduce allocations in power-of-two path by replacing map with preallocated loop
1 parent 495a34e commit b9cb350

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/lib/bits.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,12 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
156156
if (isPow2(nChars)) {
157157
return () => {
158158
entropyOffset = fillEntropy(entropyOffset, entropyBuffer, entropyFunction)
159-
const codes = mapper.map((ndx: number) =>
160-
charsEncoder(valueAt(entropyOffset + ndx * nBitsPerChar, nBitsPerChar, entropyBytes))
161-
)
159+
const codes = new Array<number>(puidLen)
160+
for (let i = 0; i < puidLen; i++) {
161+
codes[i] = charsEncoder(
162+
valueAt(entropyOffset + i * nBitsPerChar, nBitsPerChar, entropyBytes)
163+
)
164+
}
162165
entropyOffset += nBitsPerPuid
163166
return String.fromCharCode(...codes)
164167
}

0 commit comments

Comments
 (0)