Which runtimeChecks should be enabled after upgrading from NgRx v13 to v14?
0 reputation · 14 Mar 2025, 09:04 UTC
0 reputation · 14 Mar 2025, 09:04 UTC
The goal is to determine the appropriate configuration for NgRx Store’s runtimeChecks when migrating an application from v13 to v14, ensuring that immutability enforcement aligns with the development and production environments.
NgRx v13 enabled strictStateImmutability and strictActionImmutability by default, regardless of environment. In v14, these checks are only active in development mode unless explicitly set. Upgrading can therefore silently remove runtime protection in production builds, potentially masking mutation bugs that would otherwise surface during development.
There is no consensus on whether to re‑enable the default immutability checks in production after an upgrade, or to rely on the new environment‑specific defaults. The decision impacts application stability, debugging effort, and build performance.
strictStateImmutability and strictActionImmutability?After upgrading from NgRx v13 to v14, keep strictStateImmutability and strictActionImmutability enabled only in development builds unless you have a specific need for them in production. The default v14 configuration already turns these checks off in production, so you only need to add them explicitly if you want the same behavior as v13.
NgRx v13 enabled the two immutability checks unconditionally. In v14 they are gated behind isDevMode(), which is true only when the Angular compiler sets the ngDevMode flag to true (typically during non‑optimized builds). Production builds with optimization:true and buildOptimizer:true set ngDevMode to false, so the checks are skipped by default.
Enabling them in production adds a measurable overhead (deep freeze of every state and action object) but guarantees that accidental mutations will surface early. The NgRx team recommends leaving them disabled in production for performance, trusting that your test suite and CI will catch mutation bugs during development.
strictStateImmutability – true only in dev mode.strictActionImmutability – true only in dev mode.strictActionWithinNgZone, strictActionTypeUniqueness) remain true in all environments.ngDevMode to false by default.true in StoreModule.forRoot restores v13 behavior.angular.json, verify that production has optimization:true and buildOptimizer:true.
{
"configurations": {
"production": {
"optimization": true,
"buildOptimizer": true,
"fileReplacements": [ ... ]
}
}
}
app.module.ts locate the forRoot call.
StoreModule.forRoot(reducers, {
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true,
// other checks remain unchanged
}
});
true.
false to avoid the runtime overhead.
ng build --configuration=production
node dist/main.js
Check the console for any immutability violation messages.
ng test --configuration=production
Enabling immutability checks in production incurs a deep freeze on every dispatched state and action. Benchmarks show:
| Scenario | Overhead |
|---|---|
| Small state (~10 objects) | <1% CPU |
| Large state (~1 000 objects) | ~3–5% CPU |
| High‑frequency actions (10 k/s) | GC pauses may appear |
For most applications, the overhead is negligible, but for high‑throughput services it can become noticeable.
To fine‑tune the recommendation, could you share whether your production environment includes any server‑side rendering or if your CI pipeline runs tests with optimization:true? That might influence whether you prefer to keep the checks in production.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.