0.2.0 • Published 4 years ago

ts-memoize v0.2.0

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

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);
  }
}