Skip to content

Commit bb9a3c1

Browse files
committed
reduce mem a bit
1 parent 39c3bec commit bb9a3c1

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

fallback/base64.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ export function toBase64(arr, isURL, padding) {
126126

127127
// TODO: can this be optimized? This only affects non-Hermes barebone engines though
128128
const mapSize = nativeEncoder ? 128 : 65_536 // we have to store 64 KiB map or recheck everything if we can't decode to byte array
129-
const _AA = 0x4141 // 'AA' string in hex, the only allowed char pair to generate 12 zero bits
130-
const _zz = 0x7a7a // 'zz' string in hex, max allowed char pair
129+
const _min = 0x2b_2b // '++' string in hex, minimal allowed
130+
const _AAm = 0x41_41 - _min // 'AA' string in hex, the only allowed char pair to generate 12 zero bits, mapped
131+
const _zz = 0x7a_7a // 'zz' string in hex, max allowed char pair, mapped
131132

132133
export function fromBase64(str, isURL) {
133134
let inputLength = str.length
@@ -156,14 +157,14 @@ export function fromBase64(str, isURL) {
156157

157158
if (nativeEncoder) {
158159
if (!helpers.fromMap16) {
159-
helpers.fromMap16 = new Uint16Array(_zz + 1) // Warning: 64 KiB
160+
helpers.fromMap16 = new Uint16Array(_zz - _min + 1) // Warning: 40 KiB
160161
const u8 = new Uint8Array(2)
161162
const u16 = new Uint16Array(u8.buffer, u8.byteOffset, 1) // for endianess-agnostic transform
162163
alphabet.forEach((c0, i0) => {
163164
u8[0] = c0.charCodeAt(0) // FIXME, we should avoid calling charCodeAt in a loop
164165
alphabet.forEach((c1, i1) => {
165166
u8[1] = c1.charCodeAt(0)
166-
helpers.fromMap16[u16[0]] = (i0 << 6) | i1
167+
helpers.fromMap16[u16[0] - _min] = (i0 << 6) | i1
167168
})
168169
})
169170
}
@@ -175,16 +176,16 @@ export function fromBase64(str, isURL) {
175176

176177
// Optional fast loop
177178
for (const mainLength16_2 = mainLength16 - 2; i < mainLength16_2; ) {
178-
const c01 = codes16[i]
179-
const c23 = codes16[i + 1]
180-
const c45 = codes16[i + 2]
181-
const c67 = codes16[i + 3]
179+
const c01 = codes16[i]-_min
180+
const c23 = codes16[i + 1]-_min
181+
const c45 = codes16[i + 2]-_min
182+
const c67 = codes16[i + 3]-_min
182183
const x01 = m16[c01]
183184
const x23 = m16[c23]
184185
const x45 = m16[c45]
185186
const x67 = m16[c67]
186-
if (!x01 && c01 !== _AA || !x23 && c23 !== _AA) throw new SyntaxError(E_CHAR)
187-
if (!x45 && c45 !== _AA || !x67 && c67 !== _AA) throw new SyntaxError(E_CHAR)
187+
if (!x01 && c01 !== _AAm || !x23 && c23 !== _AAm) throw new SyntaxError(E_CHAR)
188+
if (!x45 && c45 !== _AAm || !x67 && c67 !== _AAm) throw new SyntaxError(E_CHAR)
188189
arr[at] = x01 >> 4
189190
arr[at + 1] = ((x01 & 0xf) << 4) | (x23 >> 8)
190191
arr[at + 2] = x23 & 0xff
@@ -196,11 +197,11 @@ export function fromBase64(str, isURL) {
196197
}
197198

198199
for (; i < mainLength16; i += 2) {
199-
const c01 = codes16[i]
200-
const c23 = codes16[i + 1]
200+
const c01 = codes16[i]-_min
201+
const c23 = codes16[i + 1]-_min
201202
const x01 = m16[c01]
202203
const x23 = m16[c23]
203-
if (!x01 && c01 !== _AA || !x23 && c23 !== _AA) throw new SyntaxError(E_CHAR)
204+
if (!x01 && c01 !== _AAm || !x23 && c23 !== _AAm) throw new SyntaxError(E_CHAR)
204205
arr[at] = x01 >> 4
205206
arr[at + 1] = ((x01 & 0xf) << 4) | (x23 >> 8)
206207
arr[at + 2] = x23 & 0xff

0 commit comments

Comments
 (0)