Experimental
The type of the function that is memoized.
The function to be memoized.
Optional
options: WeakMapMemoizeOptions<ReturnType<Func>>A memoized function with a .clearCache()
method attached.
interface RootState { items: { id: number; category: string; name: string }[] }
const selectItemsByCategory = createSelector( [ (state: RootState) => state.items, (state: RootState, category: string) => category ], (items, category) => items.filter(item => item.category === category), { memoize: weakMapMemoize, argsMemoize: weakMapMemoize } )
const createSelectorWeakMap = createSelectorCreator({ memoize: weakMapMemoize, argsMemoize: weakMapMemoize })
const selectItemsByCategory = createSelectorWeakMap( [ (state: RootState) => state.items, (state: RootState, category: string) => category ], (items, category) => items.filter(item => item.category === category) )
Creates a tree of
WeakMap
-based cache nodes based on the identity of the arguments it's been called with (in this case, the extracted values from your input selectors). This allowsweakMapMemoize
to have an effectively infinite cache size. Cache results will be kept in memory as long as references to the arguments still exist, and then cleared out as the arguments are garbage-collected.Design Tradeoffs for
weakMapMemoize
:WeakMap
s.lruMemoize
, although likely a fraction slower.Use Cases for
weakMapMemoize
: