npm.io
0.2.0 • Published 6 years ago

ts-memoize

Licence
MIT
Version
0.2.0
Deps
0
Size
19 kB
Vulns
0
Weekly
0

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