Skip to content

prismamedia/ts-memoize

Repository files navigation

Typescript Memoize class method/getter decorator

npm version github actions status code style: prettier

It's almost a copy/paste of https://github.com/darrylhodgins/typescript-memoize, without the legacy part

Usage

import { doNotMemoize, Memoize } from '@prismamedia/memoize';

class Foo {
  @Memoize()
  public get myCachedGetter(): string {
    return 'OK';
  }

  @Memoize()
  public myCachedMethod(): string {
    return 'OK';
  }

  // If not provided, the "hashFunction" takes the first argument
  @Memoize()
  public myCachedMethod(input: string): string {
    return input;
  }

  // The "hashFunction" can return anything valid as a "Map" key
  @Memoize((a: number, b: number) => [a, b].join('|'))
  public myCachedMethod(a: number, b: number): number {
    return a + b;
  }

  // The "hashFunction" can return the Symbol "doNotMemoize" in order to not memoize the result
  @Memoize((a: number, b: number) => a === b ? doNotMemoize : [a, b].join('|'))
  public myMaybeCachedMethod(a: number, b: number): number {
    return a + b;
  }
}

About

A "Memoize" decorator for Typescript classes

Resources

License

Stars

Watchers

Forks

Packages

No packages published