Skip to content

memo

Creates a memoized function that caches the results of previous calls.

Syntax

typescript
memo<T extends (...args: any[]) => any>(callback: T, options?: { cacheTimeout?: number, serializer?: (args: Parameters<T>) => string }): (...args: Parameters<T>) => ReturnType<T>

Parameters

NameTypeDescription
callbackTFunction to be memoized.
optionsobject(optional) Options for memoization.
options.cacheTimeoutnumber(optional) Time in milliseconds for cache expiration.
options.serializerfunction(optional) Function to serialize the arguments (default: JSON.stringify).

Returns

TypeDescription
(...args: Parameters<T>) => ReturnType<T>Memoized function.

Example

typescript
const slowFn = (x: number) => x * 2;
const fastFn = memo(slowFn, { cacheTimeout: 1000 });
fastFn(2); // Calculates and stores
fastFn(2); // Returns from cache

Notes

  • Throws a TypeError if the arguments are invalid.
  • The cache is based on the serialization of the arguments.

References

Released under the MIT License.