Skip to content

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

Open

Description

@lskrajny-marcura

Which @ngrx/* package(s) are the source of the bug?

signals

Minimal reproduction of the bug/regression with instructions

getState builds the state object by spreading the accumulator once per key:

return Reflect.ownKeys(stateSource[STATE_SOURCE]).reduce((state, key) => {
  const value = signals[key]();
  return { ...state, [key]: value };
}, {} as State);

Every iteration copies all keys collected so far, so a call costs O(n²) in the number of state keys. patchState calls getState on every update, and so do withReducer (events plugin) and state watchers, so the cost is paid on every state change.

Reproduction (plain Node, no Angular app needed):

import { signalState, patchState, getState } from '@ngrx/signals';

for (const n of [10, 50, 100, 200, 400]) {
  const s = signalState(Object.fromEntries(Array.from({ length: n }, (_, i) => [`k${i}`, i])));
  let t = performance.now();
  for (let i = 0; i < 2000; i++) patchState(s, { k0: i });
  const p = performance.now() - t;
  t = performance.now();
  for (let i = 0; i < 2000; i++) getState(s);
  console.log(`keys=${n} patchState: ${p.toFixed(0)} ms, getState: ${(performance.now() - t).toFixed(0)} ms`);
}

Results (2000 calls each, Node 24, Apple Silicon):

keys getState today getState with a loop patchState today patchState with a loop
10 4.7 ms 1.5 ms 7.2 ms 3.9 ms
50 59 ms 6.5 ms 75 ms 20 ms
100 243 ms 15 ms 288 ms 51 ms
200 1194 ms 37 ms 1298 ms 147 ms
400 7464 ms 113 ms 7993 ms 561 ms

In a real app this shows up with large composed stores. In our case, a SignalStore with ~130 state keys (several withFeature slices plus withReducer): its unit test spec (463 tests, many dispatched events) went from 153 s to 10 s with the change below, and it was the critical path of our CI test job.

Expected behavior

getState should be linear in the number of state keys. Building the object in place gives the same result (same keys, same order, symbol keys included, a new object on every call):

export function getState<State extends object>(stateSource: StateSource<State>): State {
  const signals: Record<string | symbol, Signal<unknown>> = stateSource[STATE_SOURCE];
  const state = {} as Record<string | symbol, unknown>;
  for (const key of Reflect.ownKeys(signals)) {
    state[key] = signals[key]();
  }
  return state as State;
}

(The only behavioral difference I can think of is a state key literally named __proto__, where assignment would set the prototype; Object.defineProperty or a computed-key literal would cover it if that matters.)

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

NgRx: 22.0.1 (same code on main)
Angular: 22.1.7
Node: 24.21.0
Browser(s): any (also reproduced in Jest/jsdom)
Operating system(s): macOS, Linux (GitHub Actions)

Other information

Related but separate: #5212 (for #5183) would stop patchState from reading the whole state for object updaters. That would remove some calls, but function updaters, withReducer and state watchers would still call getState, so the quadratic cost would remain.

I would be willing to submit a PR to fix this issue

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions