@hitorisensei/memoizee-decorator
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@hitorisensei/memoizee-decorator

Description

Adds memoization decorator for methods and getters.
Uses memoizee library for memoization.
Uses hash-sum library as a default cache-key generation algorithm.

Usage

import { Memoize } from '@hitorisensei/memoizee-decorator';

class MyClass {
  @Memoize()
  public getMyValue(): number {
    console.log('My value is calculated');
    return someExpensiveCalculation();
  }
}

const myClass = new MyClass();
myClass.getMyValue(); // console logs 'My value is calculated' only once
myClass.getMyValue(); // returns cached value

Memoizee options

Decorator can be called with all memoizee options.

import { Memoize } from '@hitorisensei/memoizee-decorator';

class MyClass {
    @Memoize({
        maxAge: 1000,
        max: 100,
    })
    public getMyValue(): number {
        console.log('My value is calculated');
        return someExpensiveCalculation();
    }
}

Clearing memoization cache

import { Memoize, clearMemoization } from '@hitorisensei/memoizee-decorator';

class MyClass {
    @Memoize()
    public getMyValue(): number {
        console.log('My value is calculated');
        return someExpensiveCalculation();
    }
}

const myClass = new MyClass();
myClass.getMyValue(); // console logs 'My value is calculated' only once
myClass.getMyValue(); // returns cached value

clearMemoization(myClass, 'getMyValue'); // clears cache
myClass.getMyValue(); // console logs 'My value is calculated' again

/@hitorisensei/memoizee-decorator/

    Package Sidebar

    Install

    npm i @hitorisensei/memoizee-decorator

    Weekly Downloads

    19

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    8.61 kB

    Total Files

    5

    Last publish

    Collaborators

    • hitorisensei