Files
Malte Ubl 10b246d604 Add a runtime benchmark suite with Node-verified A/B timing
Fourteen application-shaped workloads (JSON, regex, parsing and class
dispatch, sorting, rendering, collection pipelines, async fan-out,
Map/Set graphs, allocation, numeric kernels, exceptions, CSV, and the
two multi-module build benchmark apps on large inputs).

bench:runtime builds each workload with one or two checkouts, refuses to
time any executable whose stdout, stderr, or exit status differs from
Node, interleaves baseline and candidate launches, and reports medians
with bootstrap 95% confidence intervals, binary size, and peak RSS.
2026-10-08 09:38:23 -07:00

47 lines
1.7 KiB
TypeScript

// Allocation-heavy code: binary trees built and discarded, plus a
// reducer that produces immutable state updates with object spread.
class TreeNode {
readonly left: TreeNode | null;
readonly right: TreeNode | null;
constructor(left: TreeNode | null, right: TreeNode | null) {
this.left = left;
this.right = right;
}
check(): number {
return 1 + (this.left === null ? 0 : this.left.check()) + (this.right === null ? 0 : this.right.check());
}
}
function build(depth: number): TreeNode {
return depth === 0 ? new TreeNode(null, null) : new TreeNode(build(depth - 1), build(depth - 1));
}
interface State {
count: number;
total: number;
last: string;
history: number[];
}
interface Action {
type: string;
amount: number;
}
function reduce(state: State, action: Action): State {
if (action.type === "add") return { ...state, count: state.count + 1, total: state.total + action.amount, last: action.type };
if (action.type === "reset") return { ...state, total: 0, last: action.type, history: [...state.history.slice(-4), state.total] };
return { ...state, last: action.type };
}
const scale = Number(process.argv[2] ?? "1");
const maxDepth = 14;
let checks = 0;
for (let depth = 4; depth <= maxDepth; depth += 2) {
const iterations = Math.floor((1 << (maxDepth - depth + 4)) * scale);
for (let i = 0; i < iterations; i++) checks += build(depth).check();
}
const longLived = build(maxDepth);
let state: State = { count: 0, total: 0, last: "", history: [] };
for (let i = 0; i < Math.floor(300000 * scale); i++) {
state = reduce(state, { type: i % 97 === 0 ? "reset" : i % 3 === 0 ? "noop" : "add", amount: i % 13 });
}
console.log("checks", checks, "long", longLived.check(), "state", state.count, state.total, state.history.join(","));