Skip to content

Commit d66845a

Browse files
natemoo-reclaude
andcommitted
bench: add WASM init and startup timing benchmarks
Adds bench/startup.bench.ts with 6 cases covering cold compile, instantiation, createTermNative, createInputNative, createTerm, and full module-to-first-render startup time. Also exports `bytes` from wasm.ts so the compile step can be benchmarked independently. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4139212 commit d66845a

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

bench/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import "./input.bench.ts";
22
import "./render.bench.ts";
33
import "./ops.bench.ts";
4+
import "./startup.bench.ts";

bench/startup.bench.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Bench } from "tinybench";
2+
import { withCodSpeed } from "@codspeed/tinybench-plugin";
3+
import { bytes, compiled } from "../wasm.ts";
4+
import { createTermNative } from "../term-native.ts";
5+
import { createInputNative } from "../input-native.ts";
6+
import { createTerm } from "../term.ts";
7+
import { close, grow, open, text } from "../ops.ts";
8+
import type { Op } from "../ops.ts";
9+
10+
const helloOps: Op[] = [
11+
open("root", { layout: { width: grow(), height: grow(), direction: "ttb" } }),
12+
text("Hello, World!"),
13+
close(),
14+
];
15+
16+
let bench = withCodSpeed(new Bench());
17+
18+
bench
19+
.add("wasm compile", async () => {
20+
await WebAssembly.compile(bytes);
21+
})
22+
.add("wasm instantiate", async () => {
23+
const memory = new WebAssembly.Memory({ initial: 2 });
24+
await WebAssembly.instantiate(compiled, {
25+
env: { memory },
26+
clay: {
27+
measureTextFunction() {},
28+
queryScrollOffsetFunction(ret: number) {
29+
const v = new DataView(memory.buffer);
30+
v.setFloat32(ret, 0, true);
31+
v.setFloat32(ret + 4, 0, true);
32+
},
33+
},
34+
});
35+
})
36+
.add("createTermNative (80x24)", async () => {
37+
await createTermNative(80, 24);
38+
})
39+
.add("createInputNative", async () => {
40+
await createInputNative(50);
41+
})
42+
.add("createTerm (80x24)", async () => {
43+
await createTerm({ width: 80, height: 24 });
44+
})
45+
.add("startup: createTerm + first render (80x24)", async () => {
46+
const term = await createTerm({ width: 80, height: 24 });
47+
term.render(helloOps);
48+
});
49+
50+
await bench.run();
51+
console.table(bench.table());

tasks/bundle-wasm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const wasm = await Deno.readFile("clayterm.wasm");
44
const base64 = encodeBase64(wasm);
55

66
const source = `const bin = atob("${base64}");
7-
const bytes = new Uint8Array(bin.length);
7+
export const bytes = new Uint8Array(bin.length);
88
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
99
export const compiled = await WebAssembly.compile(bytes);
1010
`;

0 commit comments

Comments
 (0)