oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.82k stars 2.55k forks source link

`@node-rs/argon2` is faster than `Bun.password`? #8409

Open huseeiin opened 5 months ago

huseeiin commented 5 months ago
import { group, bench, run } from "mitata";
import { hash } from "@node-rs/argon2";

group("hashers", () => {
  bench("Bun.password", async () => {
    await Bun.password.hash("hello");
  });

  bench("@node-rs/argon2", async () => {
    await hash("hello");
  });
});

await run();
cpu: Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
runtime: bun 1.0.25 (x64-linux)

benchmark            time (avg)             (min … max)       p75       p99      p995
------------------------------------------------------- -----------------------------
• hashers
------------------------------------------------------- -----------------------------
Bun.password     145.83 ms/iter (142.83 ms … 157.58 ms)  145.7 ms 157.58 ms 157.58 ms
@node-rs/argon2   36.42 ms/iter   (32.39 ms … 45.54 ms)  38.95 ms  45.54 ms  45.54 ms

summary for hashers
  @node-rs/argon2
   4x faster than Bun.password
Jarred-Sumner commented 5 months ago

bun probably has a more expensive default configuration

fructiferous commented 5 months ago

Including the additional cost factors:

import { group, bench, run } from "mitata";
import { hash, Algorithm } from "@node-rs/argon2";

group("hashers", () => {
  bench("Bun.password", async () => {
    await Bun.password.hash("hello", {
        algorithm: 'argon2id',
        memoryCost: 512,
        timeCost: 512,
      });
  });

  bench("@node-rs/argon2", async () => {
    await hash("hello", {
        algorithm: Algorithm.Argon2id,
        memoryCost: 512,
        timeCost: 512,

    });
  });
});

await run();

Yields the following:

cpu: Intel(R) Core(TM) i3-4360 CPU @ 3.70GHz
runtime: bun 1.0.25 (x64-linux)

benchmark            time (avg)             (min … max)       p75       p99      p995
------------------------------------------------------- -----------------------------
• hashers
------------------------------------------------------- -----------------------------
Bun.password     175.89 ms/iter (166.86 ms … 195.69 ms) 179.31 ms 195.69 ms 195.69 ms
@node-rs/argon2  127.42 ms/iter (121.01 ms … 135.33 ms) 132.02 ms 135.33 ms 135.33 ms

summary for hashers
  @node-rs/argon2
   1.38x faster than Bun.password