https://github.com/vunovati/memoize-methods
Memoize methods of a given JavaScript object
https://github.com/vunovati/memoize-methods
cache memoize memoize-decorator proxy typescript
Last synced: 4 months ago
JSON representation
Memoize methods of a given JavaScript object
- Host: GitHub
- URL: https://github.com/vunovati/memoize-methods
- Owner: Vunovati
- License: mit
- Created: 2020-01-21T09:27:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-16T04:01:07.000Z (over 3 years ago)
- Last Synced: 2024-04-24T08:16:32.680Z (about 2 years ago)
- Topics: cache, memoize, memoize-decorator, proxy, typescript
- Language: TypeScript
- Size: 433 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# memoize-methods
Memoize methods of a given JavaScript object
[](https://github.com/prettier/prettier)
[](https://npmjs.org/package/memoize-methods)
## usage
Initialize the memoization store. The store will keep the memoized objects
for as long as the originals are not garbage collected (it uses a WeakMap).
```
const memoizeMethods = createMemoizeMethods()
```
```
const memoizedUserRepository = memoizeMethods(slowUserDbRepository)
```
the first call will be as slow as if we'd called the original user repo
```
const user = await memoizedUserRepository.getUser(userId) // slow
```
the second call will return the promise from the previous call which prevents
the unnecessary repeated async call (e.g. towards a slow db)
```
const userFast = await memoizedUserRepository.getUser(userId) // fast
```