# Redux Toolkit: createSelector & Memoization

> Prevent catastrophic re-renders by memoizing derived state using Reselect 5.0 and weakMapMemoize.

Subject: State Management | Lesson 13 of 22 | 8 min read
Interactive lesson: https://codewithanimation.in/state-management/rtk-createselector-memoization
Previous: [Redux Toolkit: useDispatch & useSelector](https://codewithanimation.in/state-management/usedispatch-useselector.md) | Next: [Redux Toolkit: Data Normalization](https://codewithanimation.in/state-management/rtk-createentityadapter.md)

## What is createSelector?

`createSelector` is a utility from the Reselect library (bundled with Redux Toolkit) used to compute derived data.

Operations like `.filter()` or `.map()` return brand new array references every time they run. Without memoization, a standard selector computing derived state will fail React's strict equality (`===`) checks, forcing unnecessary component re-renders on every single state change.

## The Memoization Pipeline

`createSelector` splits logic into two phases: Input Selectors and an Output Selector.

Input Selectors extract raw data from the state. The Output Selector performs the heavy computation (e.g., filtering). When executed, Reselect compares the return values of the Input Selectors to their previous values. If nothing changed, the Output Selector is completely skipped, and the previously cached reference is returned.

## Legacy vs Modern Caching

Historically, Reselect used `lruMemoize` with a cache size of exactly 1. This caused a notorious 'cache thrashing' bug when rendering lists: if you used the same selector instance for multiple items, the cache was constantly overwritten, resulting in 100% cache misses.

Redux Toolkit 2.0 (Reselect 5.0) solved this by upgrading the default caching engine to `weakMapMemoize`.

## Common mistake: The weakMapMemoize Architecture

`weakMapMemoize` builds a multi-level cache tree using JavaScript `WeakMap`s. This provides an effectively infinite cache size without any memory leaks.

Because it relies on weak references, as soon as a component unmounts and an argument object is no longer used, the JavaScript Garbage Collector automatically deletes the cached result from memory.

## Tip: Strict Reference Equality Only

Because `weakMapMemoize` relies on the native behavior of `WeakMap`s, it exclusively uses strict reference equality (`===`) for argument checks. You cannot use custom equality functions (like deep equals) with the new default memoizer.

## Try it: Auto Demo: Memoization Pipeline

Tap the play button below to watch the `createSelector` pipeline in action. Observe how a cache hit skips the expensive Output Selector computation completely.

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

## Check yourself

### Question 1

How does weakMapMemoize prevent memory leaks despite having an infinite cache size?

- A. It limits the cache strictly to a maximum size of 1.
- B. It relies on JavaScript WeakMaps, allowing the Garbage Collector to automatically clean up unused arguments.
- C. It forces the developer to manually call `.clearCache()` when a component unmounts.

Correct answer: B

By utilizing `WeakMap`s, Reselect holds weak references to argument objects. When your application no longer references those objects, the JS runtime automatically deletes them from the cache.

- Why not A: That describes the legacy `lruMemoize` behavior, which caused cache thrashing in lists.
- Why not C: Memoization cleanup is fully automatic in Reselect.

## Remember this

- Derived state operations like `.map()` and `.filter()` create new references, triggering re-renders unless memoized.
- `createSelector` uses Input Selectors to extract dependencies and skips the Output Selector if dependencies haven't changed.
- RTK 2.0 uses `weakMapMemoize` as the default, providing an infinite cache size to fix list rendering bugs.
- The cache is automatically cleaned up by the JS Garbage Collector via `WeakMap`s.
