# Redux Toolkit: Data Normalization

> Flatten complex data structures into ID-based dictionaries for instant O(1) lookups.

Subject: State Management | Lesson 14 of 22 | 7 min read
Interactive lesson: https://codewithanimation.in/state-management/rtk-createentityadapter
Previous: [Redux Toolkit: createSelector & Memoization](https://codewithanimation.in/state-management/rtk-createselector-memoization.md) | Next: [Redux Toolkit: Listener Middleware](https://codewithanimation.in/state-management/rtk-createlistenermiddleware.md)

## What is Data Normalization?

In Redux, data normalization is the practice of structuring your state like a relational database. Instead of storing data in nested arrays, you flatten it into top-level dictionary objects (hash maps) keyed by unique IDs.

Redux Toolkit provides `createEntityAdapter`, a utility that automatically generates this standardized normalized state structure and provides pre-built reducer methods to manage it.

## The O(n) Array Penalty

When data is stored in a standard array, updating a specific item requires an O(n) linear search. The reducer must map over the entire array until it finds the matching ID, and then it must allocate a brand new array in memory.

In a normalized dictionary, updating an item is an O(1) constant-time direct property lookup (e.g., `state.entities[id]`). This completely eliminates array iteration overhead.

## The Generated State Structure

When you initialize a slice with `adapter.getInitialState()`, it creates an object with exactly two keys: an `ids` array and an `entities` dictionary.

The `ids` array maintains the display or sorting order, while the `entities` dictionary holds the actual data objects.

## Common mistake: Widespread UI Re-renders

Beyond CPU overhead, array mapping creates a massive React problem: because `.map()` returns a new array reference, every component subscribed to that list will be forced to re-render, even if only a single item was updated.

With a normalized structure, a list component only needs to subscribe to the `ids` array, while individual row components subscribe directly to their specific `entities[id]`. Updating one row does not change the `ids` array, successfully preventing the rest of the list from re-rendering.

## Tip: Automatic Sorting

You can pass a `sortComparer` function into `createEntityAdapter`. Whenever you add or update an entity, the adapter will automatically sort the `ids` array for you, ensuring your UI always renders items in the correct order.

## Try it: O(n) Array vs O(1) Dictionary

Compare the legacy array structure with the normalized structure generated by `createEntityAdapter`.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/rtk-createentityadapter

## Check yourself

### Question 1

Why is a normalized dictionary faster for updates than an array?

- A. Dictionaries use an O(n) search algorithm that is highly optimized by V8.
- B. Looking up an entity by its ID in a dictionary is an O(1) constant-time operation, avoiding array iteration.
- C. createEntityAdapter automatically memoizes the array using weakMapMemoize.

Correct answer: B

Accessing an object property by its key (ID) happens instantly in O(1) time, regardless of whether the store has 10 items or 100,000 items.

- Why not A: Dictionaries do not use O(n) searches; they use O(1) direct lookups.
- Why not C: Memoization is for selectors. The adapter creates a structural dictionary to speed up reducers.

## Remember this

- Normalize data to avoid storing duplicated information in deeply nested trees.
- Updating items in an array takes O(n) time and creates new array references, triggering widespread re-renders.
- `createEntityAdapter` automatically generates an `{ ids, entities }` state structure.
- Updating an entity in a dictionary takes O(1) time and isolates re-renders to just that specific item.
