Skip to content

Commit cee92e2

Browse files
bluebill1049claude
andauthored
🛺 perf: make rhf more performant (#13524)
* perf: improve onChange performance * add perf test cases * 🚅 perf: improve setValid (#13525) * perf: improve on dirty checking * test out reset * try latest cypress * test set to chrome * revert back * skip the test for now * revert back change * perf: improve setValid (#13526) * add test back * test out * perf: improve setValue (#13527) * cache valdiation mode * fix counter go negative * add test guard * improve is watch check * improve more on setValues * improve `shouldRenderFormState` check * improve getDirty check * improve more ulity functions * improve logic folder * perf: improve TypeScript compile-time performance - Cap PathImpl/ArrayPathImpl recursion at 10 path segments via Prev decrement table — bounds path union size from O(N^depth) to O(N^10) - Remove dead IsPrimitiveLike second branch (Primitive & object = never) - Remove always-false FieldErrors ternary (FieldValues extends IsAny<FieldValues>) - Collapse WatchValue array check to readonly FieldPath<T>[] (mutable arrays satisfy readonly constraint; removes one FieldPath instantiation) - Move Prev table to path/common.ts so eager.ts imports it Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * update api * update * Revert "perf: improve TypeScript compile-time performance" This reverts commit edf775b. * update api * remove comments --------- Co-authored-by: Claude Sonnet 4.6 <[email protected]>
1 parent 782313f commit cee92e2

17 files changed

Lines changed: 945 additions & 179 deletions

‎cypress/e2e/reset.cy.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ describe('form reset', () => {
3131
'',
3232
);
3333

34-
cy.get('button').click({ multiple: true });
34+
cy.contains('button', 'Add item').click();
35+
cy.get('input[name="users"]').should('have.value', 'users#1');
36+
cy.contains('button', 'button').click();
3537

3638
cy.get('input[name="firstName"]').should('have.value', 'bill');
3739
cy.get('input[name="lastName"]').should('have.value', 'luo');

‎eslint.config.mjs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export default tseslint.config(
4545
},
4646
},
4747
rules: {
48-
curly: 'error',
4948
'no-extra-boolean-cast': 'error',
5049
'cypress/unsafe-to-chain-command': 'off',
5150
'@typescript-eslint/no-non-null-assertion': 'off',

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"@types/react": "^19.2.15",
8888
"@types/react-dom": "^19.2.3",
8989
"bundlewatch": "^0.4.2",
90-
"cypress": "^15.15.0",
90+
"cypress": "^15.17.0",
9191
"eslint": "^9.39.4",
9292
"eslint-config-prettier": "^10.1.8",
9393
"eslint-plugin-cypress": "^5.4.0",

‎pnpm-lock.yaml‎

Lines changed: 39 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/__tests__/logic/isWatched.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,49 @@ describe('isWatched', () => {
124124
).toBeFalsy();
125125
});
126126

127+
it('returns true on first prefix match without iterating the full Set', () => {
128+
const watch = new Set(['a', 'b', 'c', 'd', 'e']);
129+
130+
expect(
131+
isWatched('a.child', {
132+
registerName: new Set(),
133+
mount: new Set(),
134+
disabled: new Set(),
135+
unMount: new Set(),
136+
array: new Set(),
137+
watch,
138+
focus: '',
139+
watchAll: false,
140+
}),
141+
).toBeTruthy();
142+
143+
expect(
144+
isWatched('e.child', {
145+
registerName: new Set(),
146+
mount: new Set(),
147+
disabled: new Set(),
148+
unMount: new Set(),
149+
array: new Set(),
150+
watch,
151+
focus: '',
152+
watchAll: false,
153+
}),
154+
).toBeTruthy();
155+
156+
expect(
157+
isWatched('z.child', {
158+
registerName: new Set(),
159+
mount: new Set(),
160+
disabled: new Set(),
161+
unMount: new Set(),
162+
array: new Set(),
163+
watch,
164+
focus: '',
165+
watchAll: false,
166+
}),
167+
).toBeFalsy();
168+
});
169+
127170
it('should return falsy for blur event', () => {
128171
expect(
129172
isWatched(

‎src/__tests__/logic/shouldRenderFormState.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ describe('shouldRenderFormState', () => {
6565
expect(result).toBeUndefined();
6666
});
6767

68+
it('calls Object.keys on formState exactly once regardless of which branch is taken', () => {
69+
const keysSpy = jest.spyOn(Object, 'keys');
70+
71+
// non-root, non-empty, no matching key → reaches .find() branch
72+
const proxy = { isValid: true } as ReadFormState;
73+
shouldRenderFormState({ isDirty: true }, proxy, updateFormState);
74+
75+
// Each call to shouldRenderFormState should produce exactly one
76+
// Object.keys(formState) call. The proxy may also be keyed once (isRoot
77+
// length check is skipped here), so the formState key must not appear 2-3×.
78+
const formStateKeyCalls = keysSpy.mock.calls.filter(
79+
(args) =>
80+
args[0] != null &&
81+
typeof args[0] === 'object' &&
82+
'isDirty' in (args[0] as object),
83+
);
84+
expect(formStateKeyCalls).toHaveLength(1);
85+
86+
keysSpy.mockRestore();
87+
});
88+
6889
describe('when root subscribe', () => {
6990
it('should return subscribed key name if expecting all', () => {
7091
const proxy: ReadFormState = {

0 commit comments

Comments
 (0)