https://github.com/bwca/package_merry-solutions_memoize-decorator
A function to decorate methods and memoize their results to speed up further requests done with the same arguments.
https://github.com/bwca/package_merry-solutions_memoize-decorator
decorator memoization memoize memoize-decorator performance
Last synced: 2 months ago
JSON representation
A function to decorate methods and memoize their results to speed up further requests done with the same arguments.
- Host: GitHub
- URL: https://github.com/bwca/package_merry-solutions_memoize-decorator
- Owner: Bwca
- Created: 2022-07-07T05:01:40.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-07-08T06:57:57.000Z (almost 3 years ago)
- Last Synced: 2024-04-23T23:36:46.912Z (about 1 year ago)
- Topics: decorator, memoization, memoize, memoize-decorator, performance
- Language: TypeScript
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
```bash
npm i @merry-solutions/memoize-decorator
```The decorator can accept the following payload:
```typescript
export interface MemoizePayload {
// The function that will determine a unique id for the provided arguments set, you write iy
extractUniqueId: (...args: any[]) => any;
// Pass true if you want to use WeakMap, not that it requires keys to be objects
doUseWeakMap?: boolean;
// If regular map is used, you can set timeout to clear its contents, optional
clearCacheTimeout?: number;
// For debug purposes you can pass an exta function for logging all actions
debugReporter?: (message: string, state?: Map | WeakMap | unknown) => void;
}
```Now let's assume there's some class doing some calculations:
```typescript
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:
```typescript
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 :)