mem

  • Version 9.0.1
  • Published
  • 17.5 kB
  • 2 dependencies
  • MIT license

Install

npm i mem
yarn add mem
pnpm add mem

Overview

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

Index

Functions

function mem

mem: <FunctionToMemoize extends AnyFunction, CacheKeyType>(
fn: FunctionToMemoize,
{ cacheKey, cache, maxAge }?: Options<FunctionToMemoize, CacheKeyType>
) => FunctionToMemoize;
  • [Memoize](https://en.wikipedia.org/wiki/Memoization) 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);
    memoized('foo');
    //=> 1
    // Cached as it's the same argument
    memoized('foo');
    //=> 1
    // Not cached anymore as the arguments changed
    memoized('bar');
    //=> 2
    memoized('bar');
    //=> 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](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.

    Example 1

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

Package Files (1)

Dependencies (2)

Dev Dependencies (11)

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/mem.

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