memoize-one

  • Version 7.0.0
  • Published
  • 41.2 kB
  • No dependencies
  • MIT license

Install

npm i memoize-one
yarn add memoize-one
pnpm add memoize-one

Overview

A memoization library which only remembers the latest invocation

Index

Functions

function areInputsEqual

areInputsEqual: (
newInputs: readonly unknown[],
lastInputs: readonly unknown[]
) => boolean;
  • The default equality check used by memoizeOne.

    Returns true when both lists have the same length, and each item is the same value according to Object.is.

function memoizeOne

memoizeOne: <TFunc extends (this: any, ...newArgs: any[]) => any>(
resultFn: TFunc,
isEqual?: EqualityFn<TFunc & {}>
) => MemoizedFn<TFunc>;
  • Memoize a function so that it only remembers the latest arguments and result.

    The result function is called again when this or the arguments change. By default, arguments are compared with areInputsEqual (Object.is on each argument).

    Parameter resultFn

    The function to memoize

    Parameter isEqual

    An optional custom equality function for the arguments

Type Aliases

type EqualityFn

type EqualityFn<TFunc extends (...args: any[]) => any = (...args: any[]) => any> = (
newArgs: Parameters<TFunc>,
lastArgs: Parameters<TFunc>
) => boolean;
  • A function that decides if the new arguments are equal to the last arguments. Return true to use the cached result, or false to call the result function again.

    TFunc is the function being memoized. Without it, EqualityFn accepts the arguments of any function, so you can write one equality function and reuse it.

type MemoizedFn

type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = {
/** Clear the cache, so that the next call will call the result function again */
clear: () => void;
(this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>;
};
  • A memoized version of TFunc. It has the same this type, parameters and return type as TFunc, plus a .clear() function to clear the cache.

    Other properties of TFunc are not copied. Because the call signature is rebuilt from Parameters and ReturnType, only the last overload of TFunc is kept, generic type parameters become unknown, and type predicates and assertion signatures no longer narrow.

Package Files (1)

Dependencies (0)

No dependencies.

Dev Dependencies (10)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/memoize-one.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/memoize-one)
  • HTML
    <a href="https://www.jsdocs.io/package/memoize-one"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>