70160
Ce
cel-tui / files
cel-tui / benchmarks / cell-buffer.bench.ts
2.7 KB
1/** 2 * Benchmark: CellBuffer — creation, writing, and ANSI emission. 3 * 4 * Measures the cost of buffer allocation, full ANSI output generation, 5 * and differential rendering (the key to fast re-renders). 6 */ 7 8import { CellBuffer, emitBuffer } from "@cel-tui/core"; 9import { bench, group, run } from "mitata"; 10import { emitDiff } from "../packages/core/src/emitter.js"; 11import { layout } from "../packages/core/src/layout.js"; 12import { paint } from "../packages/core/src/paint.js"; 13import { appTree } from "./helpers.js"; 14 15// --- Buffer creation --- 16 17group("CellBuffer: creation", () => { 18 bench("80×24", () => { 19 new CellBuffer(80, 24); 20 }); 21 22 bench("120×40", () => { 23 new CellBuffer(120, 40); 24 }); 25 26 bench("200×50", () => { 27 new CellBuffer(200, 50); 28 }); 29}); 30 31// --- emitBuffer (full render) --- 32 33group("emitBuffer: full render", () => { 34 // Prepare a painted buffer 35 const tree80 = layout(appTree(20), 80, 24); 36 const buf80 = new CellBuffer(80, 24); 37 paint(tree80, buf80); 38 39 const tree120 = layout(appTree(50), 120, 40); 40 const buf120 = new CellBuffer(120, 40); 41 paint(tree120, buf120); 42 43 bench("80×24 app tree", () => { 44 emitBuffer(buf80); 45 }); 46 47 bench("120×40 app tree", () => { 48 emitBuffer(buf120); 49 }); 50}); 51 52// --- emitDiff (differential render) --- 53 54group("emitDiff: no changes", () => { 55 const tree = layout(appTree(20), 80, 24); 56 const buf = new CellBuffer(80, 24); 57 paint(tree, buf); 58 // Clone the buffer by creating identical content 59 const buf2 = new CellBuffer(80, 24); 60 paint(tree, buf2); 61 62 bench("80×24 identical buffers", () => { 63 emitDiff(buf, buf2); 64 }); 65}); 66 67group("emitDiff: partial changes", () => { 68 const tree1 = layout(appTree(20), 80, 24); 69 const buf1 = new CellBuffer(80, 24); 70 paint(tree1, buf1); 71 72 const buf2 = new CellBuffer(80, 24); 73 paint(tree1, buf2); 74 const changed = buf2.get(10, 5); 75 buf2.set(10, 5, { ...changed, char: changed.char === "X" ? "Y" : "X" }); 76 if (buf1.diff(buf2).length !== 1) { 77 throw new Error("Partial-diff benchmark must change exactly one cell"); 78 } 79 80 bench("80×24 one cell changed", () => { 81 emitDiff(buf1, buf2); 82 }); 83}); 84 85group("emitDiff: full change", () => { 86 const buf1 = new CellBuffer(80, 24); 87 const buf2 = new CellBuffer(80, 24); 88 const baseCell = { 89 fgColor: null, 90 bgColor: null, 91 bold: false, 92 italic: false, 93 underline: false, 94 } as const; 95 buf1.fill(0, 0, 80, 24, { ...baseCell, char: "A" }); 96 buf2.fill(0, 0, 80, 24, { ...baseCell, char: "B" }); 97 if (buf1.diff(buf2).length !== 80 * 24) { 98 throw new Error("Full-diff benchmark must change every cell"); 99 } 100 101 bench("80×24 completely different", () => { 102 emitDiff(buf1, buf2); 103 }); 104}); 105 106await run();