Skip to content

Commit

Permalink
improve base64 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Feb 8, 2024
1 parent e887217 commit 082c9a4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
1 change: 1 addition & 0 deletions .changesets/iwvpz.patch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve `base64.encode()` performance
8 changes: 4 additions & 4 deletions src/encoding/base32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ export class Base32Encoding implements Encoding {
}
): string {
let result = "";
let block = 0;
let buffer = 0;
let shift = 0;
for (let i = 0; i < data.length; i++) {
block = (block << 8) | data[i]!;
buffer = (buffer << 8) | data[i]!;
shift += 8;
while (shift >= 5) {
shift -= 5;
result += this.alphabet[(block >> shift) & 0x1f];
result += this.alphabet[(buffer >> shift) & 0x1f];
}
}
if (shift > 0) {
result += this.alphabet[(block << (5 - shift)) & 0x1f];
result += this.alphabet[(buffer << (5 - shift)) & 0x1f];
}
const includePadding = options?.includePadding ?? true;
if (includePadding) {
Expand Down
23 changes: 11 additions & 12 deletions src/encoding/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ export class Base64Encoding implements Encoding {
}
): string {
let result = "";
const chunkCount = Math.ceil(data.length / 3);
for (let i = 0; i < chunkCount; i++) {
let buffer = data[i * 3]! << 16;
buffer += (data[i * 3 + 1] ?? 0) << 8;
buffer += data[i * 3 + 2] ?? 0;
for (let j = 0; j < 4; j++) {
const key = (buffer >> ((3 - j) * 6)) & 0x3f;
result += this.alphabet[key];
let buffer = 0;
let shift = 0;
for (let i = 0; i < data.length; i++) {
buffer = (buffer << 8) | data[i]!;
shift += 8;
while (shift >= 6) {
shift += -6;
result += this.alphabet[(buffer >> shift) & 0x3f];
}
}
let padCount = 0;
if (data.length % 3 !== 0) {
padCount = 4 - Math.ceil(((data.length % 3) * 4) / 3);
if (shift > 0) {
result += this.alphabet[(buffer << (6 - shift)) & 0x3f];
}
result = result.slice(0, result.length - padCount);
const includePadding = options?.includePadding ?? true;
if (includePadding) {
const padCount = (4 - (result.length % 4)) % 4;
for (let i = 0; i < padCount; i++) {
result += "=";
}
Expand Down

0 comments on commit 082c9a4

Please sign in to comment.