Coder Social home page Coder Social logo

lgtm-migrator / memoise-decorator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alorel/memoise-decorator

0.0 0.0 0.0 2.22 MB

An ES7 decorator for memoising (caching) a method's response

License: MIT License

JavaScript 71.25% TypeScript 28.75%

memoise-decorator's Introduction

Memoise decorator

An ES7 decorator for memoising (caching) a method's response

Coverage Status


Table of Contents

Installation

npm install @aloreljs/memoise-decorator

Polyfills Required

Symbol construction via Symbol('someName') & Maps must be available.

Compatibility

  • Typescript - full
  • Spec-compliant decorator proposal - full
  • Babel (current proposal) - full
  • Babel (legacy) - full

Usage

Basic Usage

import {Memoise} from '@aloreljs/memoise-decorator';

class MyClass {
  @Memoise()
  someMethod(a, b, c) {
  }
  
  @Memoise()
  static someStaticMethod(a, b, c) {
  }
}

Custom Cache Key Generator

The decorator uses JSON.stringify on the method arguments to generate a cache key by default, which should work for most scenarios, but can be inefficient or unusable in others. You can replace it by passing your own cache key generator to the decorator function. It will be called with the class instance as an argument.

The function can return anything that's uniquely identifiable in a Map.

import {Memoise} from '@aloreljs/memoise-decorator';

class MyClass {
  constructor() {
    this.someId = 'foo';
  }
  
  @Memoise(function(label, data) {
    return `${this.someId}:${label}:${data.id}`
  })
  someMethod(label, data) {
  }
}

Using Instance Methods as Cache Key Generators

The cache generator function gets called with fn.apply(instance, methodArguments) and therefore can access instance data. The syntax is a bit different for Typescript and Babel.

Typescript

Decorators get applied after your class is already established, therefore the syntax is simple

import {Memoise} from '@aloreljs/memoise-decorator';

class MyClass {
  public readonly multiplier = 5;
  
  @Memoise(MyClass.prototype.serialiser)
  public method(num: number): string {
    return `The number is ${num}`;
  }
  
  private serialiser(num: number): number {
    return num * this.multiplier;
  }
}

Babel

Decorators get applied as part of the class definition, therefore you need to wrap the cache key generator:

import {Memoise} from '@aloreljs/memoise-decorator';

class MyClass {
  constructor() {
    this.multiplier = 5;
  }
  
  @Memoise(function() { // Arrow functions will not work
    return MyClass.prototype.serialiser.apply(this, arguments);
  })
  method(num) {
    return `The number is ${num}`;
  }
  
  serialiser(num) {
    return num * this.multiplier;
  }
}

Memoising all method calls disregarding parameters

This might be useful for methods that don't accept parameters in the first place.

import {Memoise} from '@aloreljs/memoise-decorator';

class MyClass {
  @Memoise.all()
  method() {
    return 'foo';
  }
}

Direct access to the cache map

After being called at least once, the method will get a MEMOISE_CACHE property containing the cache.

import {MEMOISE_CACHE, Memoise} from '@aloreljs/memoise-decorator';

class MyClass {
  @Memoise()
  method(a) {
    return a + 10;
  }
}
const instance = new MyClass();
console.log(instance.method[MEMOISE_CACHE]); // Undefined
instance.method(instance.method(1));
console.log(instance.method[MEMOISE_CACHE]); // Map([['[1]', 11]])

memoise-decorator's People

Contributors

greenkeeper[bot] avatar greenkeeperio-bot avatar alorel avatar dependabot[bot] avatar semantic-release-bot 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.