New Patch, Merge?

    @wendellhu/memoize
    TypeScript icon, indicating that this package has built-in type declarations

    0.1.0 • Public • Published

    memoize

    Use memoize pattern in your TypeScript code.

    Installation

    npm install @wendellhu/memoize
    
    # or yarn
    yarn add @wendellhu/memoize

    Usage

    import { memoizeFunc } from '@wendellhu/memoize';

    For function:

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

    For class:

    import { memoize } from '@wendellhu/memoize';
    
    // on getter (& setter)
    class A {
      @memoize()
      public get a() {
        return someHeavyEvaluation();
      }
    
      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 someHeavyEvaluation(param);
      }
    }
    
    // or with key
    class C {
      @memoize((param) => toKey(param))
      public getFunction(param, anotherParam) {
        // re-evaluate when arguments change, and cache by groups according to `key`
        return someHeavyEvaluation(param, anotherParam);
      }
    }

    Keywords

    none

    Install

    npm i @wendellhu/memoize

    DownloadsWeekly Downloads

    0

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    103 kB

    Total Files

    37

    Last publish

    Collaborators

    • wendellhu95