mirror of
https://github.com/github/memoize
synced 2026-07-22 20:53:41 +00:00
No description
- TypeScript 87.7%
- JavaScript 12.3%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
build(deps-dev): bump js-yaml from 4.1.1 to 4.2.0 in the npm_and_yarn group across 1 directory |
||
| .github/workflows | ||
| decorator | ||
| test | ||
| .gitignore | ||
| .npmrc | ||
| CODE_OF_CONDUCT.md | ||
| CODEOWNERS | ||
| CONTRIBUTING.md | ||
| decorator.ts | ||
| eslint.config.mjs | ||
| index.ts | ||
| LICENSE | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| SECURITY.md | ||
| tsconfig.json | ||
| vitest.config.ts | ||
memoize
This is a package which provides a memoize function, as well as a TypeScript
decorator which will memoize a class method.
Usage
import memoize from '@github/memoize'
const fn = memoize(function doExpensiveStuff() {
// Here's where you do expensive stuff!
})
const other = memoize(function doExpensiveStuff() {}, {
cache: new Map(), // pass your own cache implementation
hash: JSON.stringify // pass your own hashing implementation
})
Options:
hash?: (...args: A) => unknownProvides a single value to use as the Key for the memoization. Defaults toJSON.stringify(ish).cache?: Map<unknown, R>The Cache implementation to provide. Must be a Map or Map-alike. Defaults to a Map. Useful for replacing the cache with an LRU cache or similar.
TypeScript Decorators Support!
This package also includes a decorator module which can be used to provide TypeScript Decorator annotations to functions.
Here's an example, showing what you need to do:
import memoize from '@github/memoize/decorator'
// ^ note: add `/decorator` to the import to get decorators
class MyClass {
@memoize() // Memoize the method below
doThings() {
}
}
const cache = new Map()
class MyClass {
@memoize({ cache }) // Pass options just like the memoize function
doThings() {
}
}
Why not just use package X?
Many memoize implementations exist. This one provides all of the utility we need at GitHub and nothing more. We've used a few various implementations in the past, here are some good ones:
Development
npm install
npm test
License
Distributed under the MIT license. See LICENSE for details.