Code Monkey home page Code Monkey logo

package_merry-solutions_memoize-decorator's Introduction

Memoize Decorator

Well, what is it?

A function to decorate methods and memoize their results to speed up further requests done with the same arguments.

How to use it

npm i @merry-solutions/memoize-decorator

The decorator can accept the following payload:

export interface MemoizePayload {
  // The function that will determine a unique id for the provided arguments set, you write iy
  extractUniqueId: (...args: any[]) => any;
  // Pass true if you want to use WeakMap, not that it requires keys to be objects
  doUseWeakMap?: boolean;
  // If regular map is used, you can set timeout to clear its contents, optional
  clearCacheTimeout?: number;
  // For debug purposes you can pass an exta function for logging all actions
  debugReporter?: (message: string, state?: Map<any, unknown> | WeakMap<object, unknown> | unknown) => void;
}

Now let's assume there's some class doing some calculations:

interface CalculationPayload {
  id: number;
  someCountdownNumber: number;
}

class ObjectCountdownCalculator {
  public countdown({ someCountdownNumber }: CalculationPayload): number {
    let count = 0;
    while (count < someCountdownNumber) {
      count += 1;
    }
    return count;
  }
}

Assuming the unique arguments' identifier is the id of the passed object, we could decorate it:

import { memoize } from '@merry-solutions/memoize-decorator';

class ObjectCountdownCalculator {
  @memoize({
    extractUniqueId: (a: CalculationPayload) => a.id,
  })
  public countdown({ someCountdownNumber }: CalculationPayload): number {
    let count = 0;
    while (count < someCountdownNumber) {
      count += 1;
    }
    return count;
  }
}

And poof! Our method is now leveraging the power of memoization to reduce execution time :)

package_merry-solutions_memoize-decorator's People

Contributors

bwca avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.