Skip to content

Commit 98a7831

Browse files
authored
enhance(normalizr): Avoid hidden class mutation in normalize() result (#3878)
The normalize() return object was constructed with result: '' as any, then mutated via ret.result = visit(...). This causes a V8 hidden class transition when the property type changes from string to the actual result type (array/object/string), triggering "field type constness changed" invalidations that deoptimize code depending on this object shape. Restructured to compute the result first and construct the final NormalizedSchema in a single step, keeping the object shape stable from creation. Made-with: Cursor
1 parent e9e96f1 commit 98a7831

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@data-client/normalizr': patch
3+
---
4+
5+
Avoid hidden class mutation in normalize() return object
6+
7+
The normalize result object was constructed with `result: '' as any` then mutated via `ret.result = visit(...)`, causing a V8 hidden class transition when the property type changed from string to the actual result type. Restructured to compute the result first and construct the final object in a single step.

packages/normalizr/src/normalize/normalize.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,18 @@ See https://dataclient.io/rest/api/RestEndpoint#parseResponse for more informati
7979
}
8080
}
8181

82-
const ret: NormalizedSchema<E, R> = {
83-
result: '' as any,
82+
const state = {
8483
entities: { ...entities },
8584
indexes: { ...indexes },
8685
entitiesMeta: { ...entitiesMeta },
8786
};
88-
const visit = getVisit(new NormalizeDelegate(ret, meta));
89-
ret.result = visit(schema, input, input, undefined, args);
90-
return ret;
87+
const visit = getVisit(new NormalizeDelegate(state, meta));
88+
return {
89+
result: visit(schema, input, input, undefined, args) as any as R,
90+
entities: state.entities as any,
91+
indexes: state.indexes as any,
92+
entitiesMeta: state.entitiesMeta as any,
93+
};
9194
};
9295

9396
function expectedSchemaType(schema: Schema) {

0 commit comments

Comments
 (0)