Skip to content

Commit b1389b1

Browse files
committed
perf: move encodeLatin1 to a separate method
1 parent 9563972 commit b1389b1

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

base64.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,11 @@ if (Uint8Array.fromBase64) {
125125
if (ASCII_WHITESPACE.test(str)) throw new SyntaxError(E_CHAR) // all other chars are checked natively
126126
}
127127

128-
let raw
129128
try {
130-
raw = atob(str)
129+
arr = ascii.encodeLatin1(atob(str))
131130
} catch {
132131
throw new SyntaxError(E_CHAR) // convert atob errors
133132
}
134-
135-
const length = raw.length
136-
arr = new Uint8Array(length)
137-
for (let i = 0; i < length; i++) arr[i] = raw.charCodeAt(i)
138133
} else {
139134
return js.fromBase64(str, isBase64url) // early return to skip last chunk verification, it's already validated in js
140135
}

fallback/ascii.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,10 @@ export function decode(arr, start = 0, stop = arr.length) {
4848
const sliced = start === 0 && stop === arr.length ? arr : arr.subarray(start, stop)
4949
return String.fromCharCode.apply(String, sliced)
5050
}
51+
52+
export function encodeLatin1(str) {
53+
const length = str.length
54+
const arr = new Uint8Array(length)
55+
for (let i = 0; i < length; i++) arr[i] = str.charCodeAt(i)
56+
return arr
57+
}

0 commit comments

Comments
 (0)