ts-memoize

Use memoize pattern in your TypeScript code with only one API.

Usage no npm install needed!

<script type="module">
  import tsMemoize from 'https://cdn.skypack.dev/ts-memoize';
</script>

README

ts-memoize

Use memoize pattern in your TypeScript code with only one API.

Installation

npm install ts-memoize

# or yarn
yarn add ts-memoize

Usage

import memoize from 'ts-memoize';

For function:

const memoizedGetter = memoize((a, b) => {
  // the function should not depend on values outside of its scope
  return someHeavyRevaluation(a, b);
});

For class:

// on getter (& setter)
class A {
  @memoize
  public get a() {
    return someHeavyRevaluation();
  }

  public set value(val) {
    // re-evaluate when the setter is invoked
  }
}

// on method
class B {
  @memoize
  public getFunction(param) {
    // re-evaluate when arguments change
    return someHeavyRevaluation(param);
  }
}