mapmoize
A memoize decorator for Typescript that uses WeakMap
s to memoize results.
Heavily inspired by typescript-memoize.
Installation
npm install --save mapmoize
Usage:
@Memoize(params?: {hashFunction?: (...args: any[]) => any})
@memoize(params?: {hashFunction?: (...args: any[]) => any})
You can use it in four ways:
- Memoize a
get
accessor, - Memoize a method which takes no parameters,
- Memoize a method which varies based on all the parameters,
- Memoize a method which varies based on some combination of parameters
You can call memoized methods within the same class, too.
This could be useful if you want to memoize the return value for an entire data set,
and also a filtered or mapped version of that same set.
Memoize a get
accessor, or a method which takes no parameters
These both work the same way. Subsequent calls to a memoized method without parameters, or to a get
accessor, always return the same value.
import { memoize } from "mapmoize";
class SimpleFoo {
@memoize()
public getAllTheData() {
return data;
}
@memoize()
public get someValue() {
return value;
}
}
And then we call them from somewhere else in our code:
let simpleFoo = new SimpleFoo();
let methodVal1 = simpleFoo.getAllTheData();
let methodVal2 = simpleFoo.getAllTheData();
let getterVal1 = simpleFoo.someValue;
let getterVal2 = simpleFoo.someValue;
Memoize a method which varies based on all the parameters
Subsequent calls to this style of memoized method will always return the same value.
One thing to have in mind is that we prepare digest for the parameters by casting them to a string.
This could cause some issues since string representation of any object by default is [object Object]
.
Make sure to use custom hash function (see below) or add indicative toString
method or Symbol.toStringTag
getter.
import { memoize } from "mapmoize";
class ComplicatedFoo {
@memoize()
public getAllTheData() {
return data;
}
@memoize()
public getSomeOfTheData(id: number) {
let allTheData = this.getAllTheData();
return data;
}
@memoize()
public getGreeting(name: string, planet: string) {
return "Hello, " + name + "! Welcome to " + planet;
}
}
We call these methods from somewhere else in our code:
let complicatedFoo = new ComplicatedFoo();
let oneParam1 = complicatedFoo.getSomeOfTheData();
let oneParam2 = complicatedFoo.getSomeOfTheData();
let greeterVal1 = complicatedFoo.getGreeting("Darryl", "Earth");
let greeterVal2 = complicatedFoo.getGreeting("Darryl", "Earth");
Memoize a method which varies based on some combination of parameters
Pass in a hashFunction
which takes the same parameters as your target method, or some other custom logic.
The hashFunction
is called in the context of the method's class.
import { memoize } from "mampoize";
class MoreComplicatedFoo {
@memoize({
hashFunction: (name: string, planet: string) => name,
})
public getBetterGreeting(name: string, planet: string) {
return "Hello, " + name + "! Welcome to " + planet;
}
@memoize({
hashFunction: () => new Date().toISOString(),
})
public memoryLeak(greeting: string) {
return greeting + "!!!!!";
}
}
We call these methods from somewhere else in our code. By now you should be getting the idea:
let moreComplicatedFoo = new MoreComplicatedFoo();
let greeterVal1 = moreComplicatedFoo.getBetterGreeting("Darryl", "Earth");
let greeterVal2 = moreComplicatedFoo.getBetterGreeting("Darryl", "Mars");
let greeting = moreComplicatedFoo.memoryLeak("Hello");
Custom arguments cache
We store calculated results in a map digest(arguments) -> result
. By default it is a vanilla JS Map
,
which grows unbounded with different arguments. You could customise that by providing a custom map-like structure,
like lru-map:
import { memoize } from "mampoize";
import lru from 'lru_map'
class MoreComplicatedFoo {
@memoize({
argsCacheBuilder: () => new lru.LRUMap<string, any>(100)
})
public getBetterGreeting(name: string, planet: string) {
return "Hello, " + name + "! Welcome to " + planet;
}
}