// Streams an input through the engine in fixed-size chunks and reports peak memory.buffer.byteLength. // Usage: TYPESTRIP_ERASE_WASM=build/erase.wasm node bench/profile/stream-memory.mjs [file|--concat=50] [chunkBytes] // --concat=N builds an N MB input by concatenating corpus files (the streaming proof: memory must stay flat). import fs from "node:fs"; import { engine, transformSync } from "../../src/index.js"; import { cases } from "../../test/corpus.mjs"; const exports = engine(); const arg = process.argv[2] ?? new URL("../../node_modules/typescript/lib/lib.dom.d.ts", import.meta.url).pathname; const chunk = Number(process.argv[3] ?? 65536); let src; if (arg.startsWith("--concat=")) { const target = Number(arg.slice(9)) * 1e6; const parts = []; let total = 0; while (total < target) { for (const { src: s } of cases()) { parts.push(s.endsWith("\n") ? s : s + "\n"); total += s.length + 1; if (total >= target) break; } } src = parts.join(""); } else { src = fs.readFileSync(arg, "utf8"); } function* parts(s) { for (let i = 0; i < s.length; i += chunk) yield s.slice(i, i + chunk); } const before = exports.memory.buffer.byteLength; let peak = before; let outLen = 0; let errors = 0; const t0 = performance.now(); for (const piece of transformSync(parts(src), { onError: () => errors++ })) { outLen += piece.length; peak = Math.max(peak, exports.memory.buffer.byteLength); } const ms = performance.now() - t0; console.log(`input ${(src.length / 1e6).toFixed(2)}M chars in ${chunk}-char chunks: output ${outLen} chars, ${errors} errors, ${ms.toFixed(0)}ms, memory before ${(before / 1e6).toFixed(1)}MB peak ${(peak / 1e6).toFixed(1)}MB`);