Memoize Decorator
Well, what is it?
A function to decorate methods and memoize their results to speed up further requests done with the same arguments.
How to use it
npm i @merry-solutions/memoize-decorator
The decorator can accept the following payload:
export interface MemoizePayload {
extractUniqueId: (...args: any[]) => any;
doUseWeakMap?: boolean;
clearCacheTimeout?: number;
debugReporter?: (message: string, state?: Map<any, unknown> | WeakMap<object, unknown> | unknown) => void;
}
Now let's assume there's some class doing some calculations:
interface CalculationPayload {
id: number;
someCountdownNumber: number;
}
class ObjectCountdownCalculator {
public countdown({ someCountdownNumber }: CalculationPayload): number {
let count = 0;
while (count < someCountdownNumber) {
count += 1;
}
return count;
}
}
Assuming the unique arguments' identifier is the id of the passed object, we could decorate it:
import { memoize } from '@merry-solutions/memoize-decorator';
class ObjectCountdownCalculator {
@memoize({
extractUniqueId: (a: CalculationPayload) => a.id,
})
public countdown({ someCountdownNumber }: CalculationPayload): number {
let count = 0;
while (count < someCountdownNumber) {
count += 1;
}
return count;
}
}
And poof! Our method is now leveraging the power of memoization to reduce execution time :)