Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(encoding): improve base32 encode/decode performance #6479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 0 additions & 222 deletions encoding/_base32_common.ts

This file was deleted.

63 changes: 39 additions & 24 deletions encoding/base32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Copyright (c) 2014 Jameson Little. MIT License.
// This module is browser compatible.

import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

/**
* Utilities for
* {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6 | base32}
Expand All @@ -26,51 +23,69 @@ export type { Uint8Array_ };
*
* @module
*/
import { decode, encode } from "./_base32_common.ts";

const lookup: string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".split("");
const revLookup: number[] = [];
lookup.forEach((c, i) => (revLookup[c.charCodeAt(0)] = i));
import { calcMax, decode, encode } from "./_common32.ts";
import { detach } from "./_common_detach.ts";
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };

const padding = "=".charCodeAt(0);
const alphabet = new TextEncoder()
.encode("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567");
const rAlphabet = new Uint8Array(128).fill(32); //alphabet.length
alphabet.forEach((byte, i) => rAlphabet[byte] = i);

/**
* Decodes a base32-encoded string.
* Converts data into a base32-encoded string.
*
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6}
*
* @param b32 The base32-encoded string to decode.
* @returns The decoded data.
* @param data The data to encode.
* @returns The base32-encoded string.
*
* @example Usage
* ```ts
* import { decodeBase32 } from "@std/encoding/base32";
* import { encodeBase32 } from "@std/encoding/base32";
* import { assertEquals } from "@std/assert";
*
* assertEquals(
* decodeBase32("GZRTMMDDGA======"),
* new TextEncoder().encode("6c60c0"),
* );
* assertEquals(encodeBase32("6c60c0"), "GZRTMMDDGA======");
* ```
*/
export function decodeBase32(b32: string): Uint8Array_ {
return decode(b32, lookup);
export function encodeBase32(data: ArrayBuffer | Uint8Array | string): string {
if (typeof data === "string") {
data = new TextEncoder().encode(data) as Uint8Array_;
} else if (data instanceof ArrayBuffer) data = new Uint8Array(data).slice();
else data = data.slice();
const [output, i] = detach(
data as Uint8Array_,
calcMax((data as Uint8Array_).length),
);
encode(output, i, 0, alphabet, padding);
return new TextDecoder().decode(output);
}

/**
* Converts data into a base32-encoded string.
* Decodes a base32-encoded string.
*
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6}
*
* @param data The data to encode.
* @returns The base32-encoded string.
* @param b32 The base32-encoded string to decode.
* @returns The decoded data.
*
* @example Usage
* ```ts
* import { encodeBase32 } from "@std/encoding/base32";
* import { decodeBase32 } from "@std/encoding/base32";
* import { assertEquals } from "@std/assert";
*
* assertEquals(encodeBase32("6c60c0"), "GZRTMMDDGA======");
* assertEquals(
* decodeBase32("GZRTMMDDGA======"),
* new TextEncoder().encode("6c60c0"),
* );
* ```
*/
export function encodeBase32(data: ArrayBuffer | Uint8Array | string): string {
return encode(data, lookup);
export function decodeBase32(b32: string): Uint8Array_ {
const output = new TextEncoder().encode(b32) as Uint8Array_;
// deno-lint-ignore no-explicit-any
return new Uint8Array((output.buffer as any)
.transfer(decode(output, 0, 0, rAlphabet, padding)));
}
8 changes: 4 additions & 4 deletions encoding/base32_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ Deno.test({
name: "decodeBase32() throws on bad length",
fn() {
assertThrows(
() => decodeBase32("OOOO=="),
() => decodeBase32("OOO=="),
Error,
"Cannot decode base32 string as the length must be a multiple of 8: received length 6",
"Invalid Character (O)",
);
},
});
Expand All @@ -48,9 +48,9 @@ Deno.test({
name: "decodeBase32() throws on bad padding",
fn() {
assertThrows(
() => decodeBase32("5HXR334AQYAAAA=="),
() => decodeBase32("5HXR334AQYAAAA======="),
Error,
"Invalid pad length",
"Invalid Character (=)",
);
},
});
Expand Down
Loading