All Projects → mgechev → Memo Decorator

mgechev / Memo Decorator

Decorator which applies memoization to a method of a class.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Memo Decorator

Decko
💨 The 3 most useful ES7 decorators: bind, debounce and memoize
Stars: ✭ 1,024 (+380.75%)
Mutual labels:  decorators, memoization
Cachier
Persistent, stale-free, local and cross-machine caching for Python functions.
Stars: ✭ 359 (+68.54%)
Mutual labels:  decorators, memoization
Babel Plugin Transform Typescript Metadata
Babel plugin to emit decorator metadata like typescript compiler
Stars: ✭ 142 (-33.33%)
Mutual labels:  decorators
Joiful
TypeScript Declarative Validation for Joi
Stars: ✭ 177 (-16.9%)
Mutual labels:  decorators
Pegparser
💡 Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
Stars: ✭ 164 (-23%)
Mutual labels:  memoization
Angular Ts Decorators
A collection of angular decorators for angularjs 1.5.x projects written in typescript
Stars: ✭ 147 (-30.99%)
Mutual labels:  decorators
Sequelize Typescript
Decorators and some other features for sequelize
Stars: ✭ 2,200 (+932.86%)
Mutual labels:  decorators
Data Structures
Common data structures and algorithms implemented in JavaScript
Stars: ✭ 139 (-34.74%)
Mutual labels:  memoization
Lambda Decorators
🐍λ✨ - A collection of useful decorators for making AWS Lambda handlers
Stars: ✭ 197 (-7.51%)
Mutual labels:  decorators
Httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module
Stars: ✭ 1,930 (+806.1%)
Mutual labels:  decorators
Leetcode
High-quality LeetCode solutions
Stars: ✭ 178 (-16.43%)
Mutual labels:  memoization
Ngx Store
Angular decorators to automagically keep variables in HTML5 LocalStorage, SessionStorage, cookies; injectable services for managing and listening to data changes and a bit more.
Stars: ✭ 152 (-28.64%)
Mutual labels:  decorators
Wxa
🖖 渐进式小程序开发框架。轻量级的渐进式小程序开发框架,专注于小程序原生开发,提供更好的工程化、代码复用能力,提高开发效率并改善开发体验。
Stars: ✭ 149 (-30.05%)
Mutual labels:  decorators
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (-19.72%)
Mutual labels:  decorators
Blog
一般不会写 API 类文章,努力写有营养的文章,喜欢请点 star
Stars: ✭ 146 (-31.46%)
Mutual labels:  decorators
Strudel
A front-end framework for the back-end powered web
Stars: ✭ 180 (-15.49%)
Mutual labels:  decorators
Fitted
Simplifying http requests using ES decorators
Stars: ✭ 139 (-34.74%)
Mutual labels:  decorators
Use Inline Memo
⚛️ React hook for memoizing values inline anywhere in a component
Stars: ✭ 152 (-28.64%)
Mutual labels:  memoization
Deal
Design by contract for Python with static checker and tests' generation.
Stars: ✭ 164 (-23%)
Mutual labels:  decorators
Testdeck
Object oriented testing
Stars: ✭ 206 (-3.29%)
Mutual labels:  decorators

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].