Skip to content

fix(signals): make getState linear in the number of state keys - #5232

Open
lskrajny-marcura wants to merge 1 commit into
ngrx:mainfrom
lskrajny-marcura:fix/signals-getstate-linear
Open

lskrajny-marcura wants to merge 1 commit into
ngrx:mainfrom
lskrajny-marcura:fix/signals-getstate-linear

Conversation

@lskrajny-marcura

Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

No new tests: the output of getState is unchanged, and the existing getState / patchState / watchState specs (including the symbol-key state in state-source.spec.ts) cover it. A timing assertion would be flaky, so the benchmark is in the issue instead.

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

getState builds the state by spreading the accumulator once per key, so each call is O(n²) in the number of state keys. patchState, withReducer and state watchers call it on every state change. With 400 keys, 2000 getState calls take ~7.5 s.

Closes #5231

What is the new behavior?

getState fills one object in a loop, so it is linear. The result is the same: a new plain object with the same own keys (string and symbol) in the same order. With 400 keys, 2000 calls take ~0.11 s. Full numbers are in #5231.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Signals specs pass locally (vitest run --config modules/signals/vitest.config.mts: 889 tests). The only failing files were the migration/schematics specs that need a dist/ build first; this change does not touch them.

🤖 Generated with Claude Code

Closes ngrx#5231

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@netlify

netlify Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for ngrx-io ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 4ae031a
🔍 Latest deploy log https://app.netlify.com/projects/ngrx-io/deploys/6aea0c15ef2ebe99bab87706
😎 Deploy Preview https://deploy-preview-5232--ngrx-io.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@ngrx/signals: getState is O(n²) in the number of state keys

1 participant