• Version 10.0.0
  • Published
  • 17.9 kB
  • 2 dependencies
  • MIT license


npm i mem
yarn add mem
pnpm add mem


Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input



function mem

mem: <FunctionToMemoize extends AnyFunction, CacheKeyType>(
fn: FunctionToMemoize,
{ cacheKey, cache, maxAge }?: Options<FunctionToMemoize, CacheKeyType>
) => FunctionToMemoize;
  • [Memoize]( functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input.

    Parameter fn

    Function to be memoized.

    Example 1

    import mem from 'mem';
    let index = 0;
    const counter = () => ++index;
    const memoized = mem(counter);
    //=> 1
    // Cached as it's the same argument
    //=> 1
    // Not cached anymore as the arguments changed
    //=> 2
    //=> 2

function memClear

memClear: (fn: AnyFunction) => void;
  • Clear all cached data of a memoized function.

    Parameter fn

    Memoized function.

function memDecorator

memDecorator: <FunctionToMemoize extends AnyFunction, CacheKeyType>(
options?: Options<FunctionToMemoize, CacheKeyType>
) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
  • Returns

    A [decorator]( to memoize class methods or static class methods.

    Example 1

    import {memDecorator} from 'mem';
    class Example {
    index = 0
    counter() {
    return ++this.index;
    class ExampleWithOptions {
    index = 0
    @memDecorator({maxAge: 1000})
    counter() {
    return ++this.index;


interface Options

interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {}

    property cache

    readonly cache?: CacheStorage<CacheKeyType, ReturnType<FunctionToMemoize>>;
    • Use a different cache storage. Must implement the following methods: .has(key), .get(key), .set(key, value), .delete(key), and optionally .clear(). You could for example use a WeakMap instead or [quick-lru]( for a LRU cache.

      new Map()

      Example 1

      new WeakMap()

    property cacheKey

    readonly cacheKey?: (arguments_: Parameters<FunctionToMemoize>) => CacheKeyType;
    • Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](

      A cacheKey function can return any type supported by Map (or whatever structure you use in the cache option).

      You can have it cache **all** the arguments by value with JSON.stringify, if they are compatible:

      ``` import mem from 'mem';

      mem(function_, {cacheKey: JSON.stringify}); ```

      Or you can use a more full-featured serializer like [serialize-javascript]( to add support for RegExp, Date and so on.

      ``` import mem from 'mem'; import serializeJavascript from 'serialize-javascript';

      mem(function_, {cacheKey: serializeJavascript}); ```

      arguments_ => arguments_[0]

      Example 1

      arguments_ => JSON.stringify(arguments_)

    property maxAge

    readonly maxAge?: number;
    • Milliseconds until the cache expires.


    Package Files (1)

    Dependencies (2)

    Dev Dependencies (11)

    Peer Dependencies (0)

    No peer dependencies.


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

    You may also use to create a custom badge linking to

    • Markdown
    • HTML
      <a href=""><img src="" alt=""></a>