GithubHelp home page GithubHelp logo

mgechev / memo-decorator Goto Github PK

View Code? Open in Web Editor NEW
257.0 7.0 9.0 538 KB

Decorator which applies memoization to a method of a class.

TypeScript 89.93% JavaScript 10.07%
decorators memoization memo-decorator

memo-decorator's Introduction

Build Status

Memo Decorator

This decorator applies memoization to a method of a class.

Usage

Apply the decorator to a method of a class. The cache is local for the method but shared among all instances of the class. Strongly recommend you to use this decorator only on pure methods.

Installation:

npm i memo-decorator --save

Configuration

export interface Config {
  resolver?: Resolver;
  cache?: MapLike;
}
  • Resolver is a function, which returns the key to be used for given set of arguments. By default, the resolver will use the first argument of the method as the key.
  • MapLike is a cache instance. By default, the library would use Map.

Example:

import memo from 'memo-decorator';

class Qux {
  @memo({
    resolver: (...args: any[]) => args[1],
    cache: new WeakMap()
  })
  foo(a: number, b: number) {
    return a * b;
  }
}

Demo

import memo from 'memo-decorator';

class Qux {
  @memo()
  foo(a: number) {
    console.log('foo: called');
    return 42;
  }

  @memo({
    resolver: _ => 1
  })
  bar(a: number) {
    console.log('bar: called');
    return 42;
  }
}

const a = new Qux();
// Create a new cache entry and associate `1` with the result `42`.
a.foo(1);
// Do not invoke the original method `foo` because there's already a cache
// entry for the key `1` associated with the result of the method.
a.foo(1);
// Invoke the original `foo` because the cache doesn't contain an entry
// for the key `2`.
a.foo(2);

// Invoke `bar` and return the result `42` gotten from the original `bar` implementation.
a.bar(1);
// Does not invoke the original `bar` implementation because of the specified `resolver`
// which is passed to `memo`. For any arguments of the function, the resolver will return
// result `1` which will be used as the key.
a.bar(2);

const b = new Qux();
// Does not invoke the method `foo` because there's already an entry
// in the cache which associates the key `1` to the result `42` from the
// invocation of the method `foo` by the instance `a`.
b.foo(1);

// Outputs:
// foo: called
// foo: called
// bar: called

License

MIT

memo-decorator's People

Contributors

dependabot[bot] avatar doggy8088 avatar mboughaba avatar mgechev avatar sergeycherman avatar serkansipahi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

memo-decorator's Issues

Better explanation of the resolver

I just figured out the hard way that by default the resolver only uses the first argument passes to the function as map cache key (lodash.memorize).

This should be mentioned somewhere and may be even provide a more in depth explanation of how to write your own resolver.

How to clean the cache memory?

Hi!

Is there any way to clean the values memorized by the library? When is it cleaning them?

If I have several pages with thousands of rows in tables this can occupy a lot of memory. Isn't that a thing I should be worried about?

Thanks!

Add support for ng-add

It would be great to support ng-add command for Angular.
ng add memo-decorator

The @angular/cli suggest the following:
The package that you are trying to add does not support schematics. You can try using a different version of the package or contact the package author to add ng-add support.

Type Error when importing from a npmjs library

I have an NX workspace setup and I have been using memo-decorator for quite a bit now.

I have libraries that use memo-decorator, and applications that use those libs. Everything works perfect inside my NX workspace.

But when I am trying to publish my library to npm and then import it in an external project.
I am getting the following errors.

[Error] TypeError: memo_decorator__WEBPACK_IMPORTED_MODULE_1__ is not a function. (In 'memo_decorator__WEBPACK_IMPORTED_MODULE_1__()', 'memo_decorator__WEBPACK_IMPORTED_MODULE_1__' is an instance of Object)
	(anonymous function) (vendor.js:119720:96)
	__webpack_require__ (runtime.js:23)
	(anonymous function) (vendor.js:116631:103)
	__webpack_require__ (runtime.js:23)
	(anonymous function) (vendor.js:116822:116)
	__webpack_require__ (runtime.js:23)
	(anonymous function) (vendor.js:117104:106)
	__webpack_require__ (runtime.js:23)
	(anonymous function) (vendor.js:115324:109)
	__webpack_require__ (runtime.js:23)
	(anonymous function) (main.js:70:93)
	__webpack_require__ (runtime.js:23)
	(anonymous function) (main.js:389:93)
	__webpack_require__ (runtime.js:23)
	(anonymous function) (runtime.js:57)
	(anonymous function) (main.js:664)
	webpackJsonpCallback (runtime.js:316)
	webpackJsonpCallback
	Module Code (main.js:1:84)
	evaluate
	moduleEvaluation
	(anonymous function)
	promiseReactionJob

Is there a way of using it with arrow functions?

We generally use the following syntax for our angular classes:

class foo {

    bar = () =>{

        return 'foo'

    }

}

is there a way to use the memo decorator in methods declared like this? Currently, it throws an error if I just put it before the method declaration.

Repo still alive?

There are several PRs for updating dependencies, which are almost 1 year old. Is this repository still maintained? :)

edit: Btw very nice presentation in 2018 on ng-conf ("Optimizing an Angular application"), just realized it was you who presented and and mentioned your own npm package =)

IE11 "Syntax error"

When IE11 tries to load this package, it throws an error "Syntax error". In a debug version of an Angular application, this occurs at:

eval("\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst memoize = __webpack_require__(\"../../../../lodash.memoize/index.js\");\nexports.default = (resolver) => (target, key, descriptor) => {\n if (typeof descriptor.value !== 'function') {\n throw new Error('Memoization can be applied only to methods');\n }\n descriptor.value = memoize(descriptor.value, resolver);\n return descriptor;\n};\n//# sourceMappingURL=index.js.map//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkM6L0dpdC9EaXJlY3RvcnlTZXJ2aWNlL1VJL3BpLWRpcmVjdG9yeS9ub2RlX21vZHVsZXMvbWVtby1kZWNvcmF0b3IvaW5kZXguanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7QUFDQSw4Q0FBOEMsY0FBYztBQUM1RDtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0EiLCJmaWxlIjoiLi4vLi4vLi4vLi4vbWVtby1kZWNvcmF0b3IvaW5kZXguanMuanMiLCJzb3VyY2VzQ29udGVudCI6WyJcInVzZSBzdHJpY3RcIjtcbk9iamVjdC5kZWZpbmVQcm9wZXJ0eShleHBvcnRzLCBcIl9fZXNNb2R1bGVcIiwgeyB2YWx1ZTogdHJ1ZSB9KTtcbmNvbnN0IG1lbW9pemUgPSByZXF1aXJlKCdsb2Rhc2gubWVtb2l6ZScpO1xuZXhwb3J0cy5kZWZhdWx0ID0gKHJlc29sdmVyKSA9PiAodGFyZ2V0LCBrZXksIGRlc2NyaXB0b3IpID0+IHtcbiAgICBpZiAodHlwZW9mIGRlc2NyaXB0b3IudmFsdWUgIT09ICdmdW5jdGlvbicpIHtcbiAgICAgICAgdGhyb3cgbmV3IEVycm9yKCdNZW1vaXphdGlvbiBjYW4gYmUgYXBwbGllZCBvbmx5IHRvIG1ldGhvZHMnKTtcbiAgICB9XG4gICAgZGVzY3JpcHRvci52YWx1ZSA9IG1lbW9pemUoZGVzY3JpcHRvci52YWx1ZSwgcmVzb2x2ZXIpO1xuICAgIHJldHVybiBkZXNjcmlwdG9yO1xufTtcbi8vIyBzb3VyY2VNYXBwaW5nVVJMPWluZGV4LmpzLm1hcFxuXG5cbi8vLy8vLy8vLy8vLy8vLy8vL1xuLy8gV0VCUEFDSyBGT09URVJcbi8vIEM6L0dpdC9EaXJlY3RvcnlTZXJ2aWNlL1VJL3BpLWRpcmVjdG9yeS9ub2RlX21vZHVsZXMvbWVtby1kZWNvcmF0b3IvaW5kZXguanNcbi8vIG1vZHVsZSBpZCA9IC4uLy4uLy4uLy4uL21lbW8tZGVjb3JhdG9yL2luZGV4LmpzXG4vLyBtb2R1bGUgY2h1bmtzID0gdmVuZG9yIl0sInNvdXJjZVJvb3QiOiJ3ZWJwYWNrOi8vLyJ9\n//# sourceURL=webpack-internal:///../../../../memo-decorator/index.js\n");

Running this in the console in IE resulted in the same error.

I may be missing a polyfill, but it's unclear to me what I'm missing.

memoized method is not bound to instance

Memoized method is bound to prototype, but not to instance as expected.

class S {
  run = false;
  @memo() go() {
    this.run = true;
  }
}

it('should mark as run after go', () => {
  const s1 = new S();
  s1.go();
  expect(s1.run).toEqual(true);  // ok
});

it('should also mark as run after go', () => {
  const s2 = new S();
  s2.go();
  expect(s2.run).toEqual(true);  // Expected false to equal true.
});

it('should not mark as run without go', () => {
  const s3 = new S();
  // skipping s3.go();
  expect(s3.run).toEqual(false); // ok
});

In my application in the production code this is not a problem, but it ruins the tests.

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.