Skip to content

Commit 89d0939

Browse files
committed
perf: use Int8Array instead of Map in alphabet.decode
1 parent e95be95 commit 89d0939

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ function alphabet(letters: string | string[]): Coder<Uint8Array, string[]> {
9191
astrArr('alphabet', lettersA);
9292

9393
// mapping "b" to 1
94-
const indexes = new Map(lettersA.map((l, i) => [l, i]));
94+
const indexes = new Int8Array(256).fill(-1);
95+
lettersA.forEach((l, i) => {
96+
indexes[l.codePointAt(0)!] = i;
97+
});
9598
return {
9699
encode: (digits: Uint8Array): string[] => {
97100
abytes(digits);
@@ -111,8 +114,9 @@ function alphabet(letters: string | string[]): Coder<Uint8Array, string[]> {
111114
let at = 0
112115
for (const letter of input) {
113116
astr('alphabet.decode', letter);
114-
const i = indexes.get(letter);
115-
if (i === undefined) throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
117+
const c = letter.codePointAt(0)!;
118+
const i = indexes[c]!;
119+
if (letter.length !== 1 || c > 256 || i < 0) throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
116120
out[at++] = i;
117121
}
118122
return out;

0 commit comments

Comments
 (0)