Base83 decode

Benchmark created on


Setup

const digitLookup = new Uint8Array(128);
for (let i = 0; i < 83; i++) {
    digitLookup[
        '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~'.charCodeAt(
            i,
        )
    ] = i;
}
const decode83lookup = (str, start, end) => {
    let value = 0;
    while (start < end) {
        value *= 83;
        value += digitLookup[str.charCodeAt(start++)];
    }
    return value;
};

const digit =
    '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~';

const decode83 = (str, start, end) => {
    let value = 0;
    while (start < end) {
        value *= 83;
        value += digit.indexOf(str[start++]);
    }
    return value;
};

const characters =
    '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~';
function randomBlurHash() {
    let result = '|EHV6n';
    for (var i = 0; i < 160; i++) {
        result += characters.charAt(
            Math.floor(Math.random() * characters.length),
        );
    }
    return result;
}
let a = 'aa';

Test runner

Ready to run.

Testing in
TestOps/sec
lookup
a = decode83lookup(randomBlurHash())
ready
indexof
a = decode83(randomBlurHash())
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.