ohash icon indicating copy to clipboard operation
ohash copied to clipboard

test(serialize): improved benchmarking

Open zsilbi opened this issue 9 months ago โ€ข 3 comments

I created a small util to automatically download specific versions from GitHub to compare with each other and the current version.

import { bench } from "vitest";
import { getVersions } from "./fixtures/utils/versions";

// Options for v1
const hashOptions = { unorderedArrays: true, unorderedSets: true };
const versions = await getVersions(["v1.1.5", "v2.0.4", "v2.0.11"]);
for (const version of versions) { 
  bench(version.name, () => {
    version.serialize("hello world", hashOptions); 
  });
}

Added custom benchmarks. Added the same benchmarks for bun using mitata. Added bench:bun command to run the bun benchmark.

zsilbi avatar Mar 04 '25 21:03 zsilbi

Tip: we can use "ohash-v1": "npm:ohash@^1" in devDependencies to have same effect.

pi0 avatar Mar 05 '25 09:03 pi0

Tip: we can use "ohash-v1": "npm:ohash@^1" in devDependencies to have same effect.

This allows me to use any branch name, commit hash or version tag to benchmark with the current dev version (and against each other). If I only wanted to bench dev with v1 that would have been an easier solution of course ๐Ÿ‘๐Ÿป

For example:

export const benchConfig: BenchConfig = {
  versions: [
    "v2.0.11",
    "7a52c4ac82c33396c2e2ea90e2896ccf3a03256b",
    "main",
  ],
  ...
}
benchmark                   avg (min โ€ฆ max) p75 / p99    (min โ€ฆ top 1%)
------------------------------------------- -------------------------------
โ€ข count:1, size:small
------------------------------------------- -------------------------------
ohash @ v2.0.11              497.56 ns/iter 477.08 ns โ–†โ–ˆ
                    (457.46 ns โ€ฆ 905.65 ns) 841.28 ns โ–ˆโ–ˆ
                    (  0.00  b โ€ฆ 726.00  b)  12.63  b โ–ˆโ–ˆโ–ƒโ–โ–โ–โ–โ–โ–โ–โ–โ–โ–โ–โ–โ–โ–‚โ–โ–โ–‚โ–

ohash @ 7a52c4a              453.85 ns/iter 431.23 ns โ–ˆโ–…
                    (415.98 ns โ€ฆ 825.99 ns) 787.21 ns โ–ˆโ–ˆ
                    (  0.00  b โ€ฆ   1.03 kb)  31.17  b โ–ˆโ–ˆโ–‚โ–‚โ–โ–โ–โ–โ–โ–โ–โ–โ–‚โ–โ–‚โ–โ–โ–โ–‚โ–โ–

ohash @ main                 475.14 ns/iter 448.85 ns โ–ˆ
                      (431.98 ns โ€ฆ 1.01 ยตs) 851.39 ns โ–ˆโ–ˆ
                    (  0.00  b โ€ฆ   1.29 kb)  36.44  b โ–ˆโ–ˆโ–โ–โ–โ–‚โ–โ–โ–โ–โ–โ–โ–‚โ–‚โ–โ–โ–โ–โ–โ–โ–

ohash @ dev                  406.66 ns/iter 386.36 ns โ–ˆโ–ƒ
                    (370.59 ns โ€ฆ 962.38 ns) 769.31 ns โ–ˆโ–ˆ
                    (  0.00  b โ€ฆ   1.10 kb)  29.73  b โ–ˆโ–ˆโ–‚โ–โ–โ–โ–โ–โ–โ–โ–โ–โ–โ–‚โ–โ–โ–โ–โ–โ–โ–

summary
  ohash @ dev
   1.12x faster than ohash @ 7a52c4a
   1.17x faster than ohash @ main
   1.22x faster than ohash @ v2.0.11

zsilbi avatar Mar 05 '25 09:03 zsilbi

I decided to remove vitest benchmarks for serialize and use only mitata for both Bun and node for easier maintainabilty. Added jiti as dev. dep. to use mitata with node. Added the option to use arguments to turn on specific benchmarks (--presets (default) / --combined / --custom / --full).

zsilbi avatar Mar 09 '25 15:03 zsilbi