xonecas.io
70160
Ce

cel-tui / files

cel-tui / benchmarks / hit-test.bench.ts

1.7 KB
1/**
2 * Benchmark: hit testing and focus collection.
3 *
4 * Measures the cost of spatial lookups used for mouse click/scroll
5 * routing and keyboard focus traversal.
6 */
7
8import { HStack, Text, VStack } from "@cel-tui/core";
9import type { Node } from "@cel-tui/types";
10import { bench, group, run } from "mitata";
11import { collectFocusable, hitTest } from "../packages/core/src/hit-test.js";
12import { layout } from "../packages/core/src/layout.js";
13import { appTree, flatTree, nestedTree } from "./helpers.js";
14
15const W = 120;
16const H = 40;
17
18// --- hitTest ---
19
20group("hitTest", () => {
21  const flatL = layout(flatTree(100), W, H);
22  bench("flat tree (100 children), center", () => {
23    hitTest(flatL, 60, 20);
24  });
25
26  const nestedL = layout(nestedTree(4, 3), W, H);
27  bench("nested tree (121 nodes), center", () => {
28    hitTest(nestedL, 60, 20);
29  });
30
31  const appL = layout(appTree(50), W, H);
32  bench("app tree (50 messages), corner", () => {
33    hitTest(appL, 0, 0);
34  });
35
36  bench("app tree (50 messages), center", () => {
37    hitTest(appL, 60, 20);
38  });
39
40  bench("app tree (50 messages), miss", () => {
41    hitTest(appL, 200, 200);
42  });
43});
44
45// --- collectFocusable ---
46
47group("collectFocusable", () => {
48  // Build a tree with many focusable elements
49  const buttons: Node[] = [];
50  for (let i = 0; i < 100; i++) {
51    buttons.push(HStack({ onClick: () => {} }, [Text(`Button ${i}`)]));
52  }
53  const focusableTree = VStack({ height: "100%" }, buttons);
54  const focusableL = layout(focusableTree, W, H);
55
56  bench("100 focusable buttons", () => {
57    collectFocusable(focusableL);
58  });
59
60  const appL = layout(appTree(50), W, H);
61  bench("app tree (few focusables)", () => {
62    collectFocusable(appL);
63  });
64});
65
66await run();