React & Modern Frontend (445 Qs)•Part 10: Modern React 18, React 19 & Next.js Patterns (317 - 353)
Why is normalized state important for performance?
- O(1) lookups by ID instead of scanning/
find()-ing through an array every time a component needs one item. - Updates are localized: changing one entity only replaces that entity's entry in the
entitiesmap, so components subscribed to other entities don't see a changed reference and don't re-render. - No duplicate data to keep in sync: with nested/duplicated data, updating a single author's name might require updating it in every post that embeds it; normalized state updates it once.
- Cheaper equality checks: since each entity is a small, independent object, memoized selectors (
createSelector) anduseSelectorreference comparisons stay cheap and precise instead of comparing/deep-cloning large nested trees. - Simpler reducers: adding/removing/updating one item is a straightforward map operation, rather than mapping/filtering over deeply nested arrays.
Practical Code Example
javascript
// Normalized State: Avoids duplicate nested data and O(N) updates:
// Normalized schema:
const state = {
users: {
byId: { 'u1': { id: 'u1', name: 'Alex' } },
allIds: ['u1']
}
};