Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/getdependency-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@data-client/normalizr': patch
---

Replace megamorphic computed dispatch in getDependency with switch

`getDependency` used `delegate[array[index]](...spread)` which creates a temporary array, a computed property lookup, and a spread call on every invocation — a megamorphic pattern that prevents V8 from inlining or type-specializing the call site. Replaced with a `switch` on `path.length` for monomorphic dispatch.
14 changes: 10 additions & 4 deletions packages/normalizr/src/delegate/BaseDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ export abstract class BaseDelegate {

export const getDependency =
(delegate: BaseDelegate) =>
(path: QueryPath): object | undefined =>
delegate[['', 'getEntitiesObject', 'getEntity', 'getIndex'][path.length]](
...path,
);
(path: QueryPath): object | undefined => {
switch (path.length) {
case 1:
return delegate['getEntitiesObject'](path[0]);
case 2:
return delegate.getEntity(path[0], path[1]);
case 3:
return delegate.getIndex(path[0], path[1], path[2]);
}
};
Loading