Skip to content

Commit 0e2c009

Browse files
committed
test: benchmark retained props proxies
> 🤖🔧 ai generated
1 parent d4fa03a commit 0e2c009

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { bench, describe } from "vitest";
2+
import { mergeProps, splitProps } from "../src/index.js";
3+
import { createStore } from "../store/src/index.js";
4+
5+
// Each operation represents a batch of mounted component instances, not one call.
6+
// Keep results reachable between iterations; replace the previous batch to bound memory.
7+
const instanceCount = 10_000;
8+
type Props = { id: number; value: number; disabled: boolean; title: string };
9+
type Retained = { read: Pick<Props, "id" | "value" | "disabled">; text: { title: string } };
10+
const defaults = { disabled: true, title: "Default" };
11+
const selectedKeys = ["id", "value", "disabled"] as const;
12+
13+
const scenarios = [
14+
{
15+
name: "mergeProps",
16+
create: (source: Props): Retained => {
17+
const merged = mergeProps(defaults, source);
18+
return { read: merged, text: merged };
19+
},
20+
proxiesPerInstance: 1
21+
},
22+
{
23+
name: "splitProps",
24+
create: (source: Props): Retained => {
25+
const [selected, rest] = splitProps(source, selectedKeys);
26+
return { read: selected, text: rest };
27+
},
28+
proxiesPerInstance: 2
29+
},
30+
{
31+
name: "mergeProps+splitProps",
32+
create: (source: Props): Retained => {
33+
const [selected, rest] = splitProps(mergeProps(defaults, source), selectedKeys);
34+
return { read: selected, text: rest };
35+
},
36+
proxiesPerInstance: 3
37+
}
38+
];
39+
40+
for (const scenario of scenarios) {
41+
for (const operation of ["create", "read"] as const) {
42+
describe(`${scenario.name}-retained(${instanceCount})-${operation}`, () => {
43+
let sources: Props[] = [];
44+
let retained: Retained[] = [];
45+
let checksum = 0;
46+
47+
const setup = () => {
48+
// Real store proxies, one distinct source per instance; setup is untimed.
49+
sources = Array.from(
50+
{ length: instanceCount },
51+
(_, id) => createStore({ id, value: id, disabled: false, title: "Title" })[0]
52+
);
53+
if (operation === "read") retained = sources.map(scenario.create);
54+
};
55+
56+
const teardown = () => {
57+
// Consume observable results outside the timed callback and check the batch.
58+
if (
59+
retained.length !== instanceCount ||
60+
retained[instanceCount - 1].read.id !== instanceCount - 1
61+
)
62+
throw new Error("Incomplete retained props batch");
63+
if (
64+
operation === "read" &&
65+
checksum !== instanceCount * (instanceCount - 1) + instanceCount * 5
66+
)
67+
throw new Error("Unexpected retained props checksum");
68+
sources = [];
69+
retained = [];
70+
};
71+
72+
bench(
73+
`${operation}: ${instanceCount * scenario.proxiesPerInstance} utility proxies`,
74+
() => {
75+
if (operation === "create") {
76+
retained = sources.map(scenario.create);
77+
} else {
78+
let sum = 0;
79+
for (const entry of retained) {
80+
sum += entry.read.id + entry.read.value + Number(entry.read.disabled);
81+
sum += entry.text.title.length;
82+
}
83+
checksum = sum;
84+
}
85+
},
86+
{ time: 1000, warmupTime: 250, iterations: 20, setup, teardown }
87+
);
88+
});
89+
}
90+
}

0 commit comments

Comments
 (0)