Skip to content

Commit 9f465d5

Browse files
test(web,signals): tier-1 coverage for the headless-UI props chain
Every element a component library like Kobalte renders is reached through a stack of merge(defaults) → omit(consumed) → mergeProps(call-site statics) layers ending in a polymorphic `dynamic(() => props.as)` and one spread. Nothing in the repo measured or pinned that shape: the existing utilities and spread benches exercise one merge, one omit, or one spread in isolation, and each looks cheap. The chain is where the cost lives — every layer re-processes every key of the layer below — and it was invisible to CodSpeed. One shared fixture (`test/harness/polymorphic.tsx`: Dialog.Trigger → Button.Root → Polymorphic, plus a compiled floor twin rendering the same `<a>`) feeds all of: - `web/test/polymorphic-chain.bench.tsx` — DOM lane: mount+clear 1k rows, update 10th ×16, owner-drift gated. Chain vs compiled floor. - `web/test/server/polymorphic-chain.bench.tsx` — SSR lane: renderToString of 200 rows, chain vs floor. Construction cost only; no update phase to amortize it. - `signals/tests/store/props-chain.bench.ts` — the pure merge/omit chain at depth 1/3/7, build and build+consume reported separately so a change that moves cost between construction and read is visible. - Parity-harness scenarios `polymorphic-chain` and `polymorphic-chain-compiled-floor`: hydration ids through the chain, element identity across an update, adopt-all, no separators. - `web/test/polymorphic-chain.spec.tsx` / `server/polymorphic-chain.spec.tsx`: the chain resolves to the floor's exact attribute set (shadowing through the layers, consumed keys and `as` absent), reactive attrs stay live, the element is kept across updates; SSR serializes the same set with keys. - `utilities.test.ts`: chain-shaped semantics for merge/omit — shadowing order, hidden keys accumulating through nested omits and surviving re-merges, live reads through every layer, spread-copy snapshot, and the descriptor kind (data vs getter) surviving to the bottom of the chain. Baseline on next, per element: SSR chain ≈18× the compiled floor (≈19 µs vs ≈1 µs), DOM mount ≈2.5×, DOM update ≈2.3×; signals chain ≈21 µs at depth 3, ≈65 µs at depth 7. These are the numbers the props view work is measured against. Co-authored-by: Cursor <[email protected]>
1 parent 63560a1 commit 9f465d5

10 files changed

Lines changed: 754 additions & 0 deletions
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Tier-1 bench for the props-plumbing chain of a headless-UI component stack,
2+
// at the signals layer (no renderer): the pure cost of composing `merge()`
3+
// and `omit()` the way component libraries do.
4+
//
5+
// Every element such a library renders is reached through the same shape,
6+
// repeated once per component layer:
7+
//
8+
// props ← compiled call-site object: data properties for static
9+
// attributes, getters for reactive ones
10+
// merge(defaults, props) ← component defaults
11+
// omit(merged, ...consumedKeys) ← keys the component handles itself
12+
// merge({…staticAttrs}, rest) ← the compiler's mergeProps for
13+
// `<Child a="x" b={y()} {...rest} />`
14+
//
15+
// and the element at the bottom enumerates the result and reads every key
16+
// once (what `spread` / `ssrElement` do). The existing utilities bench
17+
// measures one merge or one omit in isolation; each looks cheap. The chain
18+
// is where the cost lives, because every layer re-processes every key of the
19+
// layer below. Depth 3 is a Kobalte `Dialog.Trigger` → `Button.Root` →
20+
// `Polymorphic` stack; depth 7 is a deeply composed app component on top.
21+
//
22+
// "build" is what a component instantiation costs; "build + consume" adds the
23+
// element's single pass over the result. Both are reported so a change that
24+
// moves cost between construction and read (eager copy vs lazy view) is
25+
// visible rather than hidden in a total.
26+
27+
import { bench, describe } from "vitest";
28+
import { createSignal, merge, omit } from "../../src/index.js";
29+
30+
const [label] = createSignal("row");
31+
const [open] = createSignal(false);
32+
33+
/** A compiled `<Trigger as="a" class="btn" … aria-label={label()} …>` props object. */
34+
function userProps(): Record<string, any> {
35+
return {
36+
as: "a",
37+
class: "btn",
38+
id: "t",
39+
href: "#row",
40+
"data-x": "1",
41+
tabIndex: 0,
42+
onClick() {},
43+
get "aria-label"() {
44+
return label();
45+
},
46+
get title() {
47+
return label();
48+
},
49+
get disabled() {
50+
return open();
51+
},
52+
get children() {
53+
return label();
54+
}
55+
};
56+
}
57+
58+
/** One component layer: defaults in, two keys consumed, static + reactive attrs added at the call site. */
59+
function layer(props: Record<string, any>, i: number): Record<string, any> {
60+
const merged = merge({ type: "button", [`default${i}`]: i }, props);
61+
const rest = omit(merged, "type", `default${i}`);
62+
return merge(
63+
{
64+
[`data-layer${i}`]: "",
65+
get [`aria-l${i}`]() {
66+
return open() ? "true" : "false";
67+
}
68+
},
69+
rest
70+
);
71+
}
72+
73+
function chain(depth: number): Record<string, any> {
74+
let props = userProps();
75+
for (let i = 0; i < depth; i++) props = layer(props, i);
76+
// The polymorphic renderer at the bottom: hide `as`, read the rest.
77+
return omit(props, "as");
78+
}
79+
80+
/** What the element does with the result: enumerate once, read each key once. */
81+
function consume(props: Record<string, any>): number {
82+
let n = 0;
83+
for (const key in props) if (props[key] !== undefined) n++;
84+
return n;
85+
}
86+
87+
// No owner needed: no function sources, so merge() creates no memos here.
88+
for (const depth of [1, 3, 7]) {
89+
describe(`props chain depth ${depth}`, () => {
90+
let sink: any;
91+
bench("build", () => {
92+
sink = chain(depth);
93+
});
94+
bench("build + consume", () => {
95+
sink = consume(chain(depth));
96+
});
97+
void sink;
98+
});
99+
}

‎packages/signals/tests/store/utilities.test.ts‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,132 @@ describe("omit Props", () => {
592592
});
593593
});
594594

595+
// The shape headless-UI libraries (Kobalte) compose per element: a compiled
596+
// props object → merge(defaults) → omit(consumed) → merge(call-site statics)
597+
// … → omit("as") at the polymorphic renderer. These pin what the chain must
598+
// mean regardless of whether the layers are eager copies or lazy views.
599+
describe("props chain (component-library shape)", () => {
600+
function compiledProps(label: () => string, open: () => boolean) {
601+
return {
602+
as: "a",
603+
class: "btn",
604+
href: "#row",
605+
get "aria-label"() {
606+
return label();
607+
},
608+
get disabled() {
609+
return open();
610+
}
611+
};
612+
}
613+
614+
function buttonRoot(props: Record<string, any>) {
615+
// Button.Root: defaults in, consume type/disabled, add derived attrs at the call site.
616+
const merged = merge({ type: "button" }, props);
617+
const others = omit(merged, "type", "disabled");
618+
const isButton = () => (merged.as ?? "button") === "button";
619+
return merge(
620+
{
621+
as: "button",
622+
get role() {
623+
return isButton() ? undefined : "button";
624+
},
625+
get "data-disabled"() {
626+
return merged.disabled ? "" : undefined;
627+
}
628+
},
629+
others
630+
);
631+
}
632+
633+
function polymorphic(props: Record<string, any>) {
634+
return omit(props, "as");
635+
}
636+
637+
test("later layers shadow earlier ones and omitted keys stay hidden through re-merges", () => {
638+
const [label] = createSignal("Open");
639+
const [open] = createSignal(false);
640+
const props = compiledProps(label, open);
641+
const atPolymorphic = buttonRoot(props);
642+
// The user's as="a" (rightmost via `others`) beats Button.Root's as="button" default.
643+
expect(atPolymorphic.as).toBe("a");
644+
expect(atPolymorphic.role).toBe("button");
645+
// Consumed keys don't reappear once merged with new statics.
646+
expect("type" in atPolymorphic).toBe(false);
647+
expect("disabled" in atPolymorphic).toBe(false);
648+
expect((atPolymorphic as any).type).toBeUndefined();
649+
650+
const element = polymorphic(atPolymorphic);
651+
expect("as" in element).toBe(false);
652+
expect(Object.keys(element).sort()).toEqual(
653+
["aria-label", "class", "data-disabled", "href", "role"].sort()
654+
);
655+
});
656+
657+
test("nested omits accumulate their hidden keys", () => {
658+
const rest = omit(omit(omit({ a: 1, b: 2, c: 3, d: 4 }, "a"), "b"), "c");
659+
expect(Object.keys(rest)).toEqual(["d"]);
660+
expect("a" in rest).toBe(false);
661+
expect("b" in rest).toBe(false);
662+
expect((rest as any).c).toBeUndefined();
663+
// …and a merge on top can't resurrect them.
664+
const remerged = merge({ e: 5 }, rest);
665+
expect(Object.keys(remerged).sort()).toEqual(["d", "e"]);
666+
expect("a" in remerged).toBe(false);
667+
});
668+
669+
test("reactive reads stay live through every layer", () => {
670+
const [label, setLabel] = createSignal("Open");
671+
const [open, setOpen] = createSignal(false);
672+
const seen: string[] = [];
673+
createRoot(() => {
674+
const element = polymorphic(buttonRoot(compiledProps(label, open)));
675+
createEffect(
676+
() => `${element["aria-label"]}|${element["data-disabled"]}`,
677+
v => {
678+
seen.push(v);
679+
}
680+
);
681+
});
682+
flush();
683+
expect(seen).toEqual(["Open|undefined"]);
684+
setLabel("Close");
685+
setOpen(true);
686+
flush();
687+
expect(seen).toEqual(["Open|undefined", "Close|"]);
688+
});
689+
690+
test("a spread copy of the chain result is a plain snapshot with the surviving keys", () => {
691+
const [label] = createSignal("Open");
692+
const [open] = createSignal(false);
693+
const element = polymorphic(buttonRoot(compiledProps(label, open)));
694+
const copy = { ...element };
695+
expect(copy).toEqual({
696+
class: "btn",
697+
href: "#row",
698+
"aria-label": "Open",
699+
role: "button",
700+
"data-disabled": undefined
701+
});
702+
});
703+
704+
test("descriptor kind survives the chain: static stays data, reactive stays a getter", () => {
705+
const [label] = createSignal("Open");
706+
const [open] = createSignal(false);
707+
const element = polymorphic(buttonRoot(compiledProps(label, open)));
708+
// A consumer (spread's children fast path, isStaticProp) must be able to
709+
// tell a compiled static attribute from a reactive one at the bottom of
710+
// the chain — that distinction is the compiler's verdict and must not be
711+
// erased by the layers in between.
712+
const staticDesc = Object.getOwnPropertyDescriptor(element, "class")!;
713+
expect(staticDesc.get).toBeUndefined();
714+
expect(staticDesc.value).toBe("btn");
715+
const reactiveDesc = Object.getOwnPropertyDescriptor(element, "aria-label")!;
716+
expect(typeof reactiveDesc.get).toBe("function");
717+
expect(Object.getOwnPropertyDescriptor(element, "as")).toBeUndefined();
718+
});
719+
});
720+
595721
describe("deep", () => {
596722
// RULED (INTERNALS-STORE-STATE.md, recon-snap pin 2): pins the LEGACY graph
597723
// shape (one $TRACK dep per level). The rewrite's deep() subscribes the
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "polymorphic-chain-compiled-floor",
3+
"shell": "<ul _hk=000><li _hk=00100><a _hk=001010 role=\"button\" tabindex=\"0\" aria-haspopup=\"dialog\" aria-expanded=\"false\" data-closed=\"\" class=\"btn\" href=\"#row-0\" data-x=\"1\" aria-label=\"row-0\" title=\"row-0\">row-0</a></li><li _hk=00110><a _hk=001110 role=\"button\" tabindex=\"0\" aria-haspopup=\"dialog\" aria-expanded=\"false\" data-closed=\"\" class=\"btn\" href=\"#row-1\" data-x=\"1\" aria-label=\"row-1\" title=\"row-1\">row-1</a></li><li _hk=00120><a _hk=001210 role=\"button\" tabindex=\"0\" aria-haspopup=\"dialog\" aria-expanded=\"false\" data-closed=\"\" class=\"btn\" href=\"#row-2\" data-x=\"1\" aria-label=\"row-2\" title=\"row-2\">row-2</a></li></ul>",
4+
"rest": ""
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "polymorphic-chain",
3+
"shell": "<ul _hk=000><li _hk=00100><a _hk=0010110 role=\"button\" tabindex=\"0\" aria-haspopup=\"dialog\" aria-expanded=\"false\" data-closed class=\"btn\" href=\"#row-0\" data-x=\"1\" aria-label=\"row-0\" title=\"row-0\">row-0</a></li><li _hk=00110><a _hk=0011110 role=\"button\" tabindex=\"0\" aria-haspopup=\"dialog\" aria-expanded=\"false\" data-closed class=\"btn\" href=\"#row-1\" data-x=\"1\" aria-label=\"row-1\" title=\"row-1\">row-1</a></li><li _hk=00120><a _hk=0012110 role=\"button\" tabindex=\"0\" aria-haspopup=\"dialog\" aria-expanded=\"false\" data-closed class=\"btn\" href=\"#row-2\" data-x=\"1\" aria-label=\"row-2\" title=\"row-2\">row-2</a></li></ul>",
4+
"rest": ""
5+
}

0 commit comments

Comments
 (0)