Retain results for varying arguments
A selector is called with many different entities, IDs, or other arguments over its lifetime.
Redux Toolkit · Function
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.
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.
weakMapMemoize<Func extends (...args: any[]) => any>(func: Func): FuncA memoization function used by selector creators to cache selector results by argument identity.
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.
A selector is called with many different entities, IDs, or other arguments over its lifetime.
Selector arguments are objects whose lifetime should control whether cached entries remain reachable.
A selector needs default reference-based memoization without configuring an LRU size.
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.
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.
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.
createSelectorcreateSelectorCreatorlruMemoize