Decorator to memoize a class method.
Mem is used as caching library. When using with Typescript, don't forget to enable the flag "experimentalDecorators": true
in tsconfig.json
.
- Node >= 8
$ npm install mem-decorator --save
const memoize = require('mem-decorator');
class Fibonacci {
@memoize()
calculateSequence(sequence) {
// Fibonacci algorithm
}
}
const fib = new Fibonacci()
console.log(fib.calculateSequence(1)) // Calculation executed (1)
console.log(fib.calculateSequence(2)) // Calculation executed (2)
console.log(fib.calculateSequence(1)) // Cache hit, returning previous calculation (1)
Annotation to memoize a class method. Works for getters as well.
Required: false
Type: object
Uses the same configuration as mem
MIT © Simon Jang