Skip to content

Commit

Permalink
Change defaultMemoize to lruMemoize in different files
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Dec 1, 2023
1 parent df615f3 commit fe618e8
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 140 deletions.
68 changes: 34 additions & 34 deletions README.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/autotrackMemoize/autotrackMemoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { createCache } from './autotracking'
* in your selector on first read. Later, when the selector is called with
* new arguments, it identifies which accessed fields have changed and
* only recalculates the result if one or more of those accessed fields have changed.
* This allows it to be more precise than the shallow equality checks in `defaultMemoize`.
* This allows it to be more precise than the shallow equality checks in `lruMemoize`.
*
* __Design Tradeoffs for `autotrackMemoize`:__
* - Pros:
* - It is likely to avoid excess calculations and recalculate fewer times than `defaultMemoize` will,
* - It is likely to avoid excess calculations and recalculate fewer times than `lruMemoize` will,
* which may also result in fewer component re-renders.
* - Cons:
* - It only has a cache size of 1.
* - It is slower than `defaultMemoize`, because it has to do more work. (How much slower is dependent on the number of accessed fields in a selector, number of calls, frequency of input changes, etc)
* - It is slower than `lruMemoize`, because it has to do more work. (How much slower is dependent on the number of accessed fields in a selector, number of calls, frequency of input changes, etc)
* - It can have some unexpected behavior. Because it tracks nested field accesses,
* cases where you don't access a field will not recalculate properly.
* For example, a badly-written selector like:
Expand Down
18 changes: 9 additions & 9 deletions src/createSelectorCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
/**
* An instance of `createSelector`, customized with a given memoize implementation.
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @public
*/
Expand Down Expand Up @@ -144,7 +144,7 @@ export interface CreateSelectorFunction<
/**
* Creates a selector creator function with the specified memoization function and options for customizing memoization behavior.
*
* @param options - An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional.
* @param options - An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional.
* @returns A customized `createSelector` function.
*
* @example
Expand All @@ -166,8 +166,8 @@ export interface CreateSelectorFunction<
* )
* ```
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @see {@link https://github.com/reduxjs/reselect#createselectorcreatormemoize--options-memoizeoptions createSelectorCreator}
*
Expand All @@ -194,7 +194,7 @@ export function createSelectorCreator<
/**
* Creates a selector creator function with the specified memoization function and options for customizing memoization behavior.
*
* @param memoize - The `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @param memoize - The `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @param memoizeOptionsFromArgs - Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards.
* @returns A customized `createSelector` function.
*
Expand All @@ -212,7 +212,7 @@ export function createSelectorCreator<
* )
* ```
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
*
* @see {@link https://github.com/reduxjs/reselect#createselectorcreatormemoize--options-memoizeoptions createSelectorCreator}
*
Expand All @@ -230,8 +230,8 @@ export function createSelectorCreator<MemoizeFunction extends UnknownMemoizer>(
* @param memoizeOptionsFromArgs - Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards.
* @returns A customized `createSelector` function.
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template MemoizeOrOptions - The type of the first argument. It can either be a `memoize` function or an `options` object containing the `memoize` function.
*/
export function createSelectorCreator<
Expand Down
18 changes: 9 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export type SelectorResultArray<Selectors extends SelectorArray> =
/**
* The options object used inside `createSelector` and `createSelectorCreator`.
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object inside `createSelector` to override the original `memoize` function that was initially passed into `createSelectorCreator`.
* @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object inside `createSelector` to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`. If none was initially provided, `weakMapMemoize` will be used.
*
Expand All @@ -80,7 +80,7 @@ export interface CreateSelectorOptions<

/**
* The memoize function that is used to memoize the {@linkcode OutputSelectorFields.resultFunc resultFunc}
* inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
*
* When passed directly into `createSelector`, it overrides the `memoize` function initially passed into `createSelectorCreator`.
*
Expand All @@ -105,7 +105,7 @@ export interface CreateSelectorOptions<
/**
* The optional memoize function that is used to memoize the arguments
* passed into the output selector generated by `createSelector`
* (e.g., `defaultMemoize` or `weakMapMemoize`).
* (e.g., `lruMemoize` or `weakMapMemoize`).
*
* When passed directly into `createSelector`, it overrides the
* `argsMemoize` function initially passed into `createSelectorCreator`.
Expand Down Expand Up @@ -170,8 +170,8 @@ export interface CreateSelectorOptions<
*
* @template InputSelectors - The type of the input selectors.
* @template Result - The type of the result returned by the `resultFunc`.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @public
*/
Expand Down Expand Up @@ -244,8 +244,8 @@ export type OutputSelectorFields<
*
* @template InputSelectors - The type of the input selectors.
* @template Result - The type of the result returned by the `resultFunc`.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @public
*/
Expand Down Expand Up @@ -442,7 +442,7 @@ export type ExtractMemoizerFields<MemoizeFunction extends UnknownMemoizer> =
/**
* Represents the additional properties attached to a function memoized by `reselect`.
*
* `defaultMemoize`, `weakMapMemoize` and `autotrackMemoize` all return these properties.
* `lruMemoize`, `weakMapMemoize` and `autotrackMemoize` all return these properties.
*
* @see {@linkcode ExtractMemoizerFields ExtractMemoizerFields}
*
Expand Down
2 changes: 1 addition & 1 deletion src/weakMapMemoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export interface WeakMapMemoizeOptions<T = any> {
* - Cons:
* - There's currently no way to alter the argument comparisons.
* They're based on strict reference equality.
* - It's roughly the same speed as `defaultMemoize`, although likely a fraction slower.
* - It's roughly the same speed as `lruMemoize`, although likely a fraction slower.
*
* __Use Cases for `weakMapMemoize`:__
* - This memoizer is likely best used for cases where you need to call the
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/weakMapMemoize.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('Parametric selectors: weakMapMemoize vs others', () => {
)
})

// describe('weakMapMemoize vs defaultMemoize with maxSize', () => {
// describe('weakMapMemoize vs lruMemoize with maxSize', () => {
// const store = setupStore()
// const state = store.getState()
// const arrayOfNumbers = Array.from({ length: 30 }, (num, index) => index)
Expand Down
6 changes: 3 additions & 3 deletions test/defaultMemoize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const createSelector = createSelectorCreator({
argsMemoize: lruMemoize
})

describe('defaultMemoize', () => {
describe(lruMemoize, () => {
test('Basic memoization', () => {
let called = 0
const memoized = lruMemoize(state => {
Expand Down Expand Up @@ -75,7 +75,7 @@ describe('defaultMemoize', () => {
const anotherObject = { foo: 'bar' }
const memoized = lruMemoize(a => a, shallowEqual)

// the first call to `memoized` doesn't hit because `defaultMemoize.lastArgs` is uninitialized
// the first call to `memoized` doesn't hit because `lruMemoize.lastArgs` is uninitialized
// and so `equalityCheck` is never called
memoized(someObject)
// first call does not shallow compare
Expand All @@ -91,7 +91,7 @@ describe('defaultMemoize', () => {
This test was useful when we had a cache size of 1 previously, and always saved `lastArgs`.
But, with the new implementation, this doesn't make sense any more.
// the third call does not fall through because `defaultMemoize` passes `anotherObject` as
// the third call does not fall through because `lruMemoize` passes `anotherObject` as
// both the `newVal` and `oldVal` params. This allows `shallowEqual` to be much more performant
// than if it had passed `someObject` as `oldVal`, even though `someObject` and `anotherObject`
// are shallowly equal
Expand Down
2 changes: 1 addition & 1 deletion test/reselect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ describe('argsMemoize and memoize', () => {
).toBe(2)
selectorDefaultParametricArgsWeakMap(store.getState(), 2)
// If we call the selector with 1, then 2, then 1 and back to 2 again,
// `defaultMemoize` will recompute a total of 4 times,
// `lruMemoize` will recompute a total of 4 times,
// but weakMapMemoize will recompute only twice.
expect(selectorDefaultParametricArgsWeakMap.recomputations()).toBe(2)
expect(
Expand Down
3 changes: 2 additions & 1 deletion test/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PayloadAction } from '@reduxjs/toolkit'
import { combineReducers, configureStore, createSlice } from '@reduxjs/toolkit'
import { test } from 'vitest'
import type { lruMemoize } from '../src/defaultMemoize'
import type { AnyFunction, OutputSelector, Simplify } from '../src/types'

export interface Todo {
Expand Down Expand Up @@ -489,7 +490,7 @@ export const logRecomputations = <S extends OutputSelector>(selector: S) => {
}

export const logSelectorRecomputations = <
S extends OutputSelector<unknown, typeof defaultMemoize, any>
S extends OutputSelector<unknown, typeof lruMemoize, any>
>(
selector: S
) => {
Expand Down
Loading

0 comments on commit fe618e8

Please sign in to comment.