File size: 8,338 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | ---
id: lruMemoize
title: lruMemoize
sidebar_label: lruMemoize
hide_title: true
description: 'lruMemoize'
---
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import { InternalLinks } from '@site/src/components/InternalLinks'
import { ExternalLinks } from '@site/src/components/ExternalLinks'
# `lruMemoize`
A memoization function that uses a provided equality check function to compare its inputs. This was originally known as `defaultMemoize`, and was the default inside of <InternalLinks.CreateSelector /> up through version 4.x.
It has a default cache size of 1. This means it always recalculates when the value of an argument changes. However, this can be customized as needed with a specific max cache size (since 4.1.0).
It determines if an argument has changed by calling the `equalityCheck` function. As `lruMemoize` is designed to be used with immutable data, the default `equalityCheck` function checks for changes using <ExternalLinks.ReferenceEqualityCheck text="reference equality" />:
{/* START: lruMemoize/referenceEqualityCheck.ts */}
<Tabs
groupId='language'
defaultValue='ts'
values={[
{label: 'TypeScript', value: 'ts'},
{label: 'JavaScript', value: 'js'},
]}>
<TabItem value='ts'>
```ts title="lruMemoize/referenceEqualityCheck.ts"
const referenceEqualityCheck = (previousValue: any, currentValue: any) => {
return previousValue === currentValue
}
```
</TabItem>
<TabItem value='js'>
```js title="lruMemoize/referenceEqualityCheck.js"
const referenceEqualityCheck = (previousValue, currentValue) => {
return previousValue === currentValue
}
```
</TabItem>
</Tabs>
{/* END: lruMemoize/referenceEqualityCheck.ts */}
## Parameters
| Name | Description |
| :----------------------- | :-------------------------------------------------------- |
| `func` | The function to be memoized. |
| `equalityCheckOrOptions` | Either an equality check function or an `options` object. |
Since 4.1.0, `lruMemoize` also accepts an options object as its first argument instead of an `equalityCheck` function. The `options` object may contain:
```ts
type EqualityFn<T = any> = (a: T, b: T) => boolean
interface LruMemoizeOptions<Result = any> {
equalityCheck?: EqualityFn
resultEqualityCheck?: EqualityFn<Result>
maxSize?: number
}
```
| Name | Description |
| :-------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `equalityCheck` | Used to compare the individual arguments of the provided calculation function. <br /> **`Default`** = `defaultEqualityCheck` |
| `resultEqualityCheck` | If provided, used to compare a newly generated output value against previous values in the cache. If a match is found, the old value is returned. This addresses the common <code>todos.map(todo => todo.id)</code> use case, where an update to another field in the original data causes a recalculation due to changed references, but the output is still effectively the same. |
| `maxSize` | The cache size for the selector. If greater than 1, the selector will use an LRU cache internally. <br /> **`Default`** = 1 |
:::warning
If `resultEqualityCheck` is used inside `argsMemoizeOptions` it has no effect.
:::
## Returns
A memoized function with a `.clearCache()` method attached.
## Type Parameters
| Name | Description |
| :----- | :----------------------------------------- |
| `Func` | The type of the function that is memoized. |
## Examples
### Using `lruMemoize` with `createSelector`
{/* START: lruMemoize/usingWithCreateSelector.ts */}
<Tabs
groupId='language'
defaultValue='ts'
values={[
{label: 'TypeScript', value: 'ts'},
{label: 'JavaScript', value: 'js'},
]}>
<TabItem value='ts'>
```ts title="lruMemoize/usingWithCreateSelector.ts"
import { shallowEqual } from 'react-redux'
import { createSelector, lruMemoize } from 'reselect'
export interface RootState {
todos: {
id: number
completed: boolean
title: string
description: string
}[]
alerts: { id: number; read: boolean }[]
}
const selectTodoIds = createSelector(
[(state: RootState) => state.todos],
todos => todos.map(todo => todo.id),
{
memoize: lruMemoize,
memoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 10
},
argsMemoize: lruMemoize,
argsMemoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 10
}
}
)
```
</TabItem>
<TabItem value='js'>
```js title="lruMemoize/usingWithCreateSelector.js"
import { shallowEqual } from 'react-redux'
import { createSelector, lruMemoize } from 'reselect'
const selectTodoIds = createSelector(
[state => state.todos],
todos => todos.map(todo => todo.id),
{
memoize: lruMemoize,
memoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 10
},
argsMemoize: lruMemoize,
argsMemoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 10
}
}
)
```
</TabItem>
</Tabs>
{/* END: lruMemoize/usingWithCreateSelector.ts */}
### Using `lruMemoize` with `createSelectorCreator`
{/* START: lruMemoize/usingWithCreateSelectorCreator.ts */}
<Tabs
groupId='language'
defaultValue='ts'
values={[
{label: 'TypeScript', value: 'ts'},
{label: 'JavaScript', value: 'js'},
]}>
<TabItem value='ts'>
```ts title="lruMemoize/usingWithCreateSelectorCreator.ts"
import { shallowEqual } from 'react-redux'
import { createSelectorCreator, lruMemoize } from 'reselect'
export interface RootState {
todos: {
id: number
completed: boolean
title: string
description: string
}[]
alerts: { id: number; read: boolean }[]
}
const createSelectorShallowEqual = createSelectorCreator({
memoize: lruMemoize,
memoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 10
},
argsMemoize: lruMemoize,
argsMemoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 10
}
})
const selectTodoIds = createSelectorShallowEqual(
[(state: RootState) => state.todos],
todos => todos.map(todo => todo.id)
)
```
</TabItem>
<TabItem value='js'>
```js title="lruMemoize/usingWithCreateSelectorCreator.js"
import { shallowEqual } from 'react-redux'
import { createSelectorCreator, lruMemoize } from 'reselect'
const createSelectorShallowEqual = createSelectorCreator({
memoize: lruMemoize,
memoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 10
},
argsMemoize: lruMemoize,
argsMemoizeOptions: {
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual,
maxSize: 10
}
})
const selectTodoIds = createSelectorShallowEqual(
[state => state.todos],
todos => todos.map(todo => todo.id)
)
```
</TabItem>
</Tabs>
{/* END: lruMemoize/usingWithCreateSelectorCreator.ts */}
|