Skip to content

Commit

Permalink
Merge pull request #55 from sno2/perf/faster-btoa
Browse files Browse the repository at this point in the history
perf(btoa): improve btoa() speed
  • Loading branch information
davidchambers authored Feb 22, 2023
2 parents 8920540 + 95d6445 commit e156e82
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,32 @@
InvalidCharacterError.prototype.name = 'InvalidCharacterError';

// encoder
// [https://gist.github.com/999166] by [https://github.com/nignag]
function btoa(input) {
var str = String (input);
for (
// initialize result and counter
var block, charCode, idx = 0, map = chars, output = '';
// if the next str index does not exist:
// change the mapping table to "="
// check if d has no fractional digits
str.charAt (idx | 0) || (map = '=', idx % 1);
// "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
output += map.charAt (63 & block >> 8 - idx % 1 * 8)
) {
charCode = str.charCodeAt (idx += 3 / 4);
if (charCode > 0xFF) {
var data = String (input), o1, o2, o3, bits, i = 0, acc = '';

while (i < data.length) {
// pack three octets into four hextets
o1 = data.charCodeAt (i++);
o2 = data.charCodeAt (i++);
o3 = data.charCodeAt (i++);

if (o1 > 128 || o2 > 128 || o3 > 128) {
throw new InvalidCharacterError ("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
}
block = block << 8 | charCode;

bits = (o1 << 16) | (o2 << 8) | o3;
// use hextets to index into b64, and append result to encoded string
acc += chars.charAt ((bits >> 18) & 0x3F) +
chars.charAt ((bits >> 12) & 0x3F) +
chars.charAt ((bits >> 6) & 0x3F) +
chars.charAt ((bits) & 0x3F);
}

switch (data.length % 3) {
case 0: return acc;
case 1: return acc.slice (0, -2) + '==';
case 2: return acc.slice (0, -1) + '=';
}
return output;
}

// decoder
Expand Down

0 comments on commit e156e82

Please sign in to comment.