Faster Than What? How I Back Every Speed Claim in MerkleForge
Reading time: 6 minutesWhen I shared the TypeScript port of MerkleForge on X, the post included a speedup number. One reply cut straight through it:
13x faster than what?
The same person asked a second, sharper question: could I prove the TypeScript port produces exactly the same bytes as the Rust crates it was ported from?
Both questions were fair, and I did not have a good enough answer to either. The numbers were real, but they lived in my terminal, not in the repo. The baseline was clear in my head and nowhere else. And “same output as Rust” was something I believed, not something a stranger could check.
This post is about what I changed in response. It is less about Merkle trees than about a habit I now treat as part of the engineering: a performance claim is a product feature, and it needs tests, sources and a way to reproduce it.
Rule 1: same bytes first
A speedup only counts if the output is identical. A faster Merkle tree that produces a different root is not faster; it is a different, broken tree.
So before any benchmark, I built a parity check. bench/parity.mjs builds the same trees the benchmark times, from the same leaves, and compares the output byte for byte against reference implementations:
| MerkleForge | Compared with | What must match |
|---|---|---|
keccak256 |
@noble/hashes |
101,025 digests, inputs of 0–1024 bytes plus random |
OpenZeppelin-compatible SimpleMerkleTree and StandardMerkleTree |
@openzeppelin/merkle-tree 1.0.8 |
root, every node, 2,000 proofs, a multiproof |
BinaryMerkleTree with WebAssembly hashing |
the same tree with pure-JS hashing | root, 2,000 encoded proofs |
BinaryMerkleTree (Keccak-256 and SHA-256) |
the Rust merkle-variants crate |
root, and the bincode bytes of 2,000 proofs |
That is 16 checks at 1,024, 16,384 and 131,072 leaves. For the Rust rows, a small Rust binary reads the exact leaves the TypeScript side generated and prints the root and the serialized proofs, so the comparison is on bytes, not on “looks the same”.
The important part is where it runs: in CI, on every pull request and push, as its own job, with the Rust crates taken live from the Rust repo’s main branch. If either side changes behaviour, the build fails. Parity stopped being a claim and became a gate.
Underneath that, the unit tests check the OpenZeppelin-compatible package against the real OpenZeppelin library on 150 random trees per type, test every adapter and codec against vectors generated by the Rust crates, and test the Solidity verifiers against fixtures produced by the TypeScript packages.
Rule 2: every ratio names its baseline
“13x faster” is meaningless without “than what”. I went back through every number and found I had been mixing three different comparisons. Now they are separate, and never mixed:
| Baseline | What is compared | Headline |
|---|---|---|
| Another JavaScript library | @merkleforge/openzeppelin vs @openzeppelin/merkle-tree, the library it replaces |
builds trees 8–15x faster, proofs 4–6x, verification 4–5x, with identical roots and proofs |
| The previous MerkleForge release | 0.2.3 vs 0.2.2, same harness | builds a 131,072-leaf Keccak-256 tree 5x faster (485 ms → 98 ms) |
| A hash implementation | MerkleForge’s keccak256 vs @noble/hashes |
about 2x faster per hash, identical digests |
And one deliberate line: there is no speed claim against the Rust crates. The TypeScript packages are a port of the Rust ones. They are checked against Rust for identical output, not raced against it. Racing a port against its own reference teaches you nothing useful and invites exactly the kind of reply I got.
Rule 3: every claim points to a table
The last piece was traceability. I added a section to BENCHMARKS.md called “Where each claim comes from”: every speed claim used anywhere in the project (the READMEs, doc comments, changesets) is listed with where it appears and the exact table that backs it.
It sounds like bureaucracy. In practice it is a one-minute check before every release: if a number is in the README, it must be in that table, and that table must come from a run anyone can repeat. When a claim cannot be traced, it gets removed, not softened.
Rule 4: anyone can rerun it
Every number in the file can be reproduced with a few commands:
pnpm install && pnpm build # in the repo root
cd bench && npm install
npm run bench # library comparison
npm run parity # byte-for-byte parity, exits non-zero on any difference
npm run bench:keccak # Keccak-256 per call
Two design choices made this practical. The benchmark lives in a standalone project outside the main workspace, because the comparison libraries are heavy (the Aztec packages alone are over 1 GB) and CI should never have to install them. And the CI parity job installs only the two libraries it compares against, not the whole benchmark setup.
Being honest about noise and fairness
Reproducible does not mean precise. On my laptop, timings vary by 10–30% between runs; the OpenZeppelin library itself measured 14.5 s and 10.5 s for the same build in two runs. So the document says it plainly: read the ratios, not the last digit.
Fairness gets the same treatment. The benchmark lists what is not like for like:
- MerkleForge hashes every leaf with a domain prefix (
H(0x00 || leaf)), which is twice the hashing work of a library that uses leaves directly, and it still builds faster. That is stated, not hidden. - The Aztec trees use Poseidon2, a SNARK-friendly hash that is more expensive in JavaScript, and keep nodes in a database with commit, rollback and snapshots. They are solving a different problem, and the numbers say so.
- Build time is the median of three builds; proof and verify times are means after a warm-up; any run where a proof fails to verify fails the benchmark.
I would rather publish a smaller, defensible number than a bigger one that falls apart under the first good question.
The performance work itself
With the guardrails in place, the actual optimisation work became much more confident, because every change was checked for identical output before it was timed. The 5x build speedup between releases came from four changes:
- Batch hashing. Hash adapters can now hash a whole tree layer in one call. The WebAssembly adapter uses this to cross into WASM once per layer instead of once per node.
- A small custom WebAssembly module. Instead of wrapping a general-purpose hashing package,
@merkleforge/hash-wasmnow embeds a 9 KBno_stdRust module with an unrolled Keccak-f[1600] permutation and, where the engine supports WebAssembly SIMD, two permutations at once. - A faster pure-JS Keccak. The JavaScript fallback uses an unrolled permutation over local variables, generated by a script rather than written by hand, which is about 2x faster per call than
@noble/hasheswith identical digests. - Flat storage.
BinaryMerkleTreekeeps every node in oneUint8Arrayinstead of one object per node, and fills padding subtrees from precomputed empty-subtree digests instead of hashing them.
| Case (131,072 leaves) | 0.2.2 | 0.2.3 | Speedup |
|---|---|---|---|
| Build, WebAssembly hashing | 485 ms | 98 ms | 5.0x |
| Build, pure JS | 2,119 ms | 637 ms | 3.3x |
| Verify one proof, WebAssembly | 40.8 µs | 17.9 µs | 2.3x |
The install footprint matters too, and it is in the same document: 1–3 dependencies and about 1.1–1.4 MB, against 38 dependencies and 16 MB for OpenZeppelin’s library and hundreds of megabytes for the Aztec packages.
What I took from it
The reply on X was a small moment, but it changed how I work:
- Correctness gates performance. Parity runs before benchmarks and blocks merges. A speedup that changes the output is a bug.
- A number without a baseline is marketing. One named baseline per ratio, and a clear line about what you are not claiming.
- Claims need sources, like code needs tests. If a number cannot be traced to a reproducible run, it does not ship.
- Publish the caveats. Noise, unfair comparisons and the cases where you are slower belong in the same document as the wins. (In the Rust write-up I kept a benchmark where specialised libraries build trees faster than MerkleForge, for the same reason.)
Open-source infrastructure runs on trust. The fastest way to earn it is to make every claim checkable, and to thank the person who asks “faster than what?”.
Links: MerkleForge-ts · BENCHMARKS.md · MerkleForge (Rust)