From 973bad0c6170d2ff2ad08cb52c574002f1d02ec8 Mon Sep 17 00:00:00 2001 From: Wolfgang Schoenberger <221313372+wolfiesch@users.noreply.github.com> Date: Wed, 22 Jul 2026 05:09:09 -0700 Subject: [PATCH] test(natives): include production isWellFormed guards in native diff timings --- packages/natives/bench/diff.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/natives/bench/diff.ts b/packages/natives/bench/diff.ts index abb3e5c1f..3db13b1a8 100644 --- a/packages/natives/bench/diff.ts +++ b/packages/natives/bench/diff.ts @@ -4,6 +4,7 @@ * Usage: bun bench/diff.ts * Records git sha, scenario, and iteration counts; prints a markdown table. */ +import * as os from "node:os"; import { diffLines as nativeDiffLines, structuredPatchHunks } from "../native/index.js"; import * as Diff from "diff"; @@ -47,8 +48,9 @@ function bench(fn: () => unknown): { meanMs: number; iterations: number } { return { meanMs: (performance.now() - start) / ITERATIONS, iterations: ITERATIONS }; } -const sha = Bun.spawnSync(["git", "rev-parse", "HEAD"]).stdout.toString().trim(); -console.log(`# diff bench — sha ${sha}, warmup ${WARMUP}, iterations ${ITERATIONS}/scenario\n`); +const sha = Bun.env.BENCH_SHA ?? Bun.spawnSync(["git", "rev-parse", "HEAD"]).stdout.toString().trim(); +console.log(`# diff bench — sha ${sha}, warmup ${WARMUP}, iterations ${ITERATIONS}/scenario`); +console.log(`# host: ${os.cpus()[0]?.model ?? "unknown"}, ${os.platform()}-${os.arch()}, bun ${Bun.version}\n`); console.log("| scenario | jsdiff diffLines | native diffLines | speedup | jsdiff structuredPatch | native hunks | speedup |"); console.log("|---|---|---|---|---|---|---|"); @@ -59,9 +61,15 @@ for (const lines of [100, 5_000, 50_000].filter(n => n <= MAX_LINES)) { const oldText = buildDoc(rng, lines); const newText = mutate(rng, oldText, density); const jsLines = bench(() => Diff.diffLines(oldText, newText)); - const natLines = bench(() => nativeDiffLines(oldText, newText)); + // Native timings include the isWellFormed() guards the production call + // sites pay before choosing the native path. + const natLines = bench(() => + oldText.isWellFormed() && newText.isWellFormed() ? nativeDiffLines(oldText, newText) : undefined, + ); const jsPatch = bench(() => Diff.structuredPatch("", "", oldText, newText, "", "", { context: 3 })); - const natPatch = bench(() => structuredPatchHunks(oldText, newText, 3)); + const natPatch = bench(() => + oldText.isWellFormed() && newText.isWellFormed() ? structuredPatchHunks(oldText, newText, 3) : undefined, + ); const fmt = (ms: number) => (ms >= 1 ? `${ms.toFixed(2)}ms` : `${(ms * 1000).toFixed(1)}µs`); console.log( `| ${lines} lines / ${density * 100}% edits | ${fmt(jsLines.meanMs)} | ${fmt(natLines.meanMs)} | ${(jsLines.meanMs / natLines.meanMs).toFixed(1)}x | ${fmt(jsPatch.meanMs)} | ${fmt(natPatch.meanMs)} | ${(jsPatch.meanMs / natPatch.meanMs).toFixed(1)}x |`,