// The SSR tree walker on a big page: one `` of N rows, five cells each, a component per row // and a `class`/`href` per cell — the shape an admin list or a data export renders. Reports wall // time and the heap the render grew by, so a walker that allocates a promise per node shows up. // // bun run scripts/bench/render-html.bench.ts [--json] [--rows=] [--out=] import { heapSize } from 'bun:jsc'; import { renderFixShellArg } from '../../packages/core/src/error-render'; import { escapeHtml } from '../../packages/core/src/html-escape'; import { h } from '../../packages/render/src/jsx'; import { renderToHtml } from '../../packages/render/src/render-html'; import { parseScriptArgs } from '../lib/args'; import { DEFAULT_RUN_TIMEOUT_MS } from '../lib/run'; import { ScriptError } from '../lib/script-error'; import { boxFacts } from './http-hello'; interface Row { readonly id: number; readonly name: string; readonly email: string; readonly status: string; readonly total: number; } const rowOf = (id: number): Row => ({ id, name: `Customer <${id}>`, email: `c${id}@example.com`, status: id % 3 === 0 ? 'active' : 'pending', total: id * 7, }); function RowView(props: { readonly row: Row }): unknown { const { row } = props; return h( 'tr', { class: 'row', 'data-id': row.id }, h('td', null, row.id), h('td', null, h('a', { href: `/customers/${row.id}` }, row.name)), h('td', null, row.email), h('td', { class: `status-${row.status}` }, () => row.status), h('td', { class: 'num' }, row.total), ); } /** * The page under test. Exported so the regression test renders exactly what the bench times. * `probe` runs as the tbody's last child — after every row, before the walk returns — and renders * nothing, so the markup is the same with or without it. */ export function tablePage(rows: number, probe?: () => null): unknown { const data: unknown[] = Array.from({ length: rows }, (_, i) => h(RowView, { row: rowOf(i) })); if (probe !== undefined) data.push(probe); return h('main', null, h('h1', null, 'Customers'), h('table', null, h('tbody', null, data))); } /** * The same markup with no walker at all: template strings over the same rows, through the same * core escaper. The yardstick the walker is held to as a RATIO, timed interleaved with it, so a * loaded machine slows both and the test stays a statement about the walker. */ export function templateBaseline(rows: number): string { let out = '

Customers

'; for (let id = 0; id < rows; id++) { const row = rowOf(id); out += `` + `` + `` + ``; } return `${out}
${row.id}${escapeHtml(row.name)}${escapeHtml(row.email)}${escapeHtml(row.status)}${row.total}
`; } export interface RenderBenchResult { readonly rows: number; readonly bytes: number; /** Best of the timed runs: the walker's cost, with the least scheduler noise. */ readonly ms: number; /** Best template-string run, timed in the same loop. */ readonly baselineMs: number; /** * Best CPU time of the walker over best CPU time of the baseline: what it costs over writing the * strings by hand. CPU, not wall clock: on a loaded machine the long side rarely gets a quiet run * while the short one does, which moved a wall-clock ratio 4.6-6.9 where this one read 5.3-5.7. */ readonly ratio: number; } /** Microseconds of CPU this process spent since `from`. */ const cpuSince = (from: NodeJS.CpuUsage): number => { const spent = process.cpuUsage(from); return spent.user + spent.system; }; const round = (value: number, places: number): number => { const scale = 10 ** places; return Math.round(value * scale) / scale; }; export async function benchRender(rows: number, runs = 5): Promise { const page = tablePage(rows); await renderToHtml(page); // warm the JIT templateBaseline(rows); let best = Number.POSITIVE_INFINITY; let baseline = Number.POSITIVE_INFINITY; let bestCpu = Number.POSITIVE_INFINITY; let baselineCpu = Number.POSITIVE_INFINITY; for (let i = 0; i < runs; i++) { let started = performance.now(); let cpu = process.cpuUsage(); (await renderToHtml(page)).length; bestCpu = Math.min(bestCpu, cpuSince(cpu)); best = Math.min(best, performance.now() - started); started = performance.now(); cpu = process.cpuUsage(); templateBaseline(rows).length; // `.length` flattens the rope, as a response body would baselineCpu = Math.min(baselineCpu, cpuSince(cpu)); baseline = Math.min(baseline, performance.now() - started); } const html = await renderToHtml(page); return { rows, bytes: html.length, ms: round(best, 2), baselineMs: round(baseline, 2), ratio: round(bestCpu / baselineCpu, 1), }; } /** * How far one render grows the heap, in MiB: live memory plus whatever garbage the collector had not * reached yet, so it swings with GC timing (≈50 MiB here, ≈100 under a smaller `forceRAMSize`). * The bench reports it; the regression test holds `liveAtEnd`, which does not swing. */ export async function heapGrowth(rows: number): Promise { const page = tablePage(rows); Bun.gc(true); const before = heapSize(); await renderToHtml(page); return round((heapSize() - before) / 1024 / 1024, 1); } /** * What the walk holds LIVE once every row is visited, in MiB: a full collection runs inside the * walk, at its last node, so the reading is the output so far plus anything the walker keeps per * node — a frame or promise per pending subtree — and no garbage, whatever the GC's timing. */ export async function liveAtEnd(rows: number): Promise { let peak = 0; const page = tablePage(rows, () => { Bun.gc(true); peak = heapSize(); return null; }); Bun.gc(true); const before = heapSize(); await renderToHtml(page); return round((peak - before) / 1024 / 1024, 1); } const HEAP_FLAG = 'heap-only'; const LIVE_FLAG = 'live-only'; /** What a fresh-process probe answered: the one number it printed, or a coded refusal. */ export function freshReading( flag: string, run: { readonly success: boolean; readonly exitedDueToTimeout?: boolean; readonly stdout: Buffer; }, timeoutMs: number, ): number { const fix = `bun run scripts/bench/render-html.bench.ts --${renderFixShellArg(flag, '')} --rows=1000`; if (run.exitedDueToTimeout === true) { throw new ScriptError({ code: 'X_SPAWN_TIMED_OUT', cause: `the --${flag} probe did not finish within ${String(timeoutMs)} ms and was killed`, fix, }); } const printed = run.stdout.toString().trim(); const value = Number(printed); // `Number('')` is 0: a probe that crashed before printing would otherwise read as no heap at all. if (!run.success || printed === '' || !Number.isFinite(value)) { throw new ScriptError({ code: 'X_BENCH_RUN_FAILED', cause: `the --${flag} probe exited ${run.success ? 'cleanly' : 'non-zero'} and printed no number`, fix, }); } return value; } function inFreshProcess(flag: string, rows: number): number { const run = Bun.spawnSync([process.execPath, import.meta.path, `--${flag}`, `--rows=${rows}`], { timeout: DEFAULT_RUN_TIMEOUT_MS, }); return freshReading(flag, run, DEFAULT_RUN_TIMEOUT_MS); } /** `heapGrowth` is only true as the first render in a process: earlier garbage would count. */ export const heapInFreshProcess = (rows: number): number => inFreshProcess(HEAP_FLAG, rows); export const liveInFreshProcess = (rows: number): number => inFreshProcess(LIVE_FLAG, rows); if (import.meta.main) { const args = parseScriptArgs(Bun.argv.slice(2)); const asked = args.flags.get('rows'); const counts = typeof asked === 'string' ? [Number(asked)] : [10_000, 50_000]; if (args.flags.get(HEAP_FLAG) === true) console.log(await heapGrowth(counts[0] ?? 0)); else if (args.flags.get(LIVE_FLAG) === true) console.log(await liveAtEnd(counts[0] ?? 0)); else { const results = []; for (const rows of counts) { results.push({ ...(await benchRender(rows)), heapMiB: heapInFreshProcess(rows), liveMiB: liveInFreshProcess(rows), }); } const data = { box: boxFacts(Bun.env), results }; const out = args.flags.get('out'); if (typeof out === 'string') await Bun.write(out, `${JSON.stringify(data, null, 2)}\n`); if (args.json) console.log(JSON.stringify(data, null, 2)); else for (const r of results) console.log( `${r.rows} rows: ${r.ms} ms (${r.ratio}× a template string), +${r.heapMiB} MiB heap`, ); } }