Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redux Toolkit · Function

weakMapMemoize

weakMapMemoize is the default Reselect memoizer used by current Redux Toolkit selectors. It caches results for every distinct argument set by reference, with object-keyed entries eligible for garbage collection and no configurable cache-size limit.

Since
RTK 2.0

How it works

weakMapMemoize is Reselect's memoization function re-exported by Redux Toolkit. It caches results separately for each distinct argument set using reference identity, with object arguments indexed through WeakMaps so entries can be reclaimed by garbage collection; primitive arguments are handled with a regular map.

Syntax

weakMapMemoize<Func extends (...args: any[]) => any>(func: Func): Func

Returns

A memoization function used by selector creators to cache selector results by argument identity.

Key points

  • Current Reselect selectors use weakMapMemoize automatically unless another memoizer is configured.
  • Each unique argument tuple receives its own memoized result, and identity rather than deep equality determines uniqueness.
  • The effectively unbounded cache is useful for selectors called repeatedly with many different argument values.

Common mistakes

  • The cache cannot be bounded through a maximum-size option; use lruMemoize instead when an explicit cache limit or custom equality is needed.

Example: Memoize selector results with weakMapMemoize

import { createSelector, weakMapMemoize } from '@reduxjs/toolkit'

type Item = { id: string; category: string }
type RootState = { items: Item[] }

const selectItems = (state: RootState) => state.items
const selectCategory = (_state: RootState, category: string) => category

const selectItemsByCategory = createSelector(
  [selectItems, selectCategory],
  (items, category) => items.filter((item) => item.category === category),
  { memoize: weakMapMemoize },
)

const state = { items: [{ id: '1', category: 'books' }] }
console.log(selectItemsByCategory(state, 'books'))

Creates a selector whose result is memoized using weakMapMemoize, allowing repeated calls with distinct argument sets without a configured cache-size limit.

When to use it

Retain results for varying arguments

A selector is called with many different entities, IDs, or other arguments over its lifetime.

Allow garbage-collectable object caches

Selector arguments are objects whose lifetime should control whether cached entries remain reachable.

Use the default selector memoizer

A selector needs default reference-based memoization without configuring an LRU size.

Interview questions

QHow does weakMapMemoize differ from a small LRU cache?

weakMapMemoize caches a selector result for each distinct argument set instead of retaining only a small recent window. Object arguments are keyed by reference, and object-keyed entries can be released when those objects are garbage-collected.

Follow-up: When should lruMemoize be preferred? Use it when custom equality comparisons or a configurable maximum cache size are required.

QWhat equality rule determines cache hits?

It uses reference comparisons only, so two separately created but structurally identical objects do not share a cache entry. This makes stable object references important for predictable memoization.

Follow-up: What alternative supports custom equality? Configure createSelector with lruMemoize and provide an equalityCheck option.

QHow is weakMapMemoize used in a Redux Toolkit application?

Redux Toolkit re-exports weakMapMemoize from Reselect, so it can be imported from @reduxjs/toolkit without installing Reselect separately. It is also the default memoizer used by current Reselect selectors.

Follow-up: How can a selector use it explicitly? Pass { memoize: weakMapMemoize } as the selector options, or construct a selector creator with it.

  • createSelector
  • createSelectorCreator
  • lruMemoize

Source: Deriving Data with Selectors. Checked 2026-10-15.