Skip to content

bun's atob is slower than deno #202

@Nugine

Description

@Nugine
running ./scripts/base64.js

node v18.4.0
b64Long:        n = 100,                dt = 11.177s,   freq = 8.947/s,         time = 111.774ms/op
b64LongE:       n = 100,                dt = 0.610s,    freq = 164.011/s,       time = 6.097ms/op
b64LongD:       n = 100,                dt = 10.505s,   freq = 9.519/s,         time = 105.053ms/op
b64Short:       n = 1000000,            dt = 1.202s,    freq = 831708.743/s,    time = 1202ns/op
b64ShortE:      n = 1000000,            dt = 0.473s,    freq = 2112092.696/s,   time = 473ns/op
b64ShortD:      n = 1000000,            dt = 0.679s,    freq = 1472274.956/s,   time = 679ns/op

deno 1.23.2
b64Long:        n = 100,                dt = 0.206s,    freq = 485.437/s,       time = 2.060ms/op
b64LongE:       n = 100,                dt = 0.110s,    freq = 909.091/s,       time = 1.100ms/op
b64LongD:       n = 100,                dt = 0.100s,    freq = 1000.000/s,      time = 1.000ms/op
b64Short:       n = 1000000,            dt = 0.462s,    freq = 2164502.165/s,   time = 462ns/op
b64ShortE:      n = 1000000,            dt = 0.226s,    freq = 4424778.761/s,   time = 226ns/op
b64ShortD:      n = 1000000,            dt = 0.224s,    freq = 4464285.714/s,   time = 224ns/op

bun 0.0.83
b64Long:        n = 100,                dt = 0.590s,    freq = 169.407/s,       time = 5.903ms/op
b64LongE:       n = 100,                dt = 0.115s,    freq = 869.085/s,       time = 1.151ms/op
b64LongD:       n = 100,                dt = 0.459s,    freq = 218.040/s,       time = 4.586ms/op
b64Short:       n = 1000000,            dt = 0.179s,    freq = 5586775.263/s,   time = 179ns/op
b64ShortE:      n = 1000000,            dt = 0.089s,    freq = 11291840.404/s,  time = 89ns/op
b64ShortD:      n = 1000000,            dt = 0.089s,    freq = 11217444.886/s,  time = 89ns/op

CPU: Intel(R) Core(TM) i5-9300H CPU @ 2.40GHz
System: Linux 5.4.0-121-generic #137-Ubuntu SMP Wed Jun 15 13:33:07 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

base64.js
// Extracted from <https://github.com/denoland/deno/blob/main/cli/bench/deno_common.js>

function bench(name, n, f) {
    const t1 = performance.now();
    for (let i = 0; i < n; ++i) {
        f(i);
    }
    const t2 = performance.now();

    const dt = (t2 - t1) / 1e3;
    const freq = n / dt;
    const time = (t2 - t1) / n;

    const msg = [
        `${name}:     \t`,
        `n = ${n},          \t`,
        `dt = ${dt.toFixed(3)}s, \t`,
        `freq = ${freq.toFixed(3)}/s, \t`,
    ];

    if (time >= 1) {
        msg.push(`time = ${time.toFixed(3)}ms/op`);
    } else {
        msg.push(`time = ${(time * 1e6).toFixed(0)}ns/op`);
    }

    console.log(msg.join(""));
}

const LONG = "helloworld".repeat(1e5);
const SHORT = "123";

function b64Long() {
    const input = LONG;
    bench("b64Long", 100, () => {
        atob(btoa(input));
    });
}

function b64LongE() {
    const input = LONG;
    bench("b64LongE", 100, () => {
        btoa(input);
    });
}

function b64LongD() {
    const input = btoa(LONG);
    bench("b64LongD", 100, () => {
        atob(input);
    });
}

function b64Short() {
    const input = SHORT;
    bench("b64Short", 1e6, () => {
        atob(btoa(input));
    });
}

function b64ShortE() {
    const input = SHORT;
    bench("b64ShortE", 1e6, () => {
        btoa(input);
    });
}

function b64ShortD() {
    const input = btoa(SHORT);
    bench("b64ShortD", 1e6, () => {
        atob(input);
    });
}

b64Long();
b64LongE();
b64LongD();
b64Short();
b64ShortE();
b64ShortD();

Metadata

Metadata

Assignees

No one assigned

    Labels

    performanceAn issue with performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions