No description
  • TypeScript 87.7%
  • JavaScript 12.3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Manuel Puyol 2ae4f46399
Merge pull request #43 from github/dependabot/npm_and_yarn/npm_and_yarn-4e38a52fea
build(deps-dev): bump js-yaml from 4.1.1 to 4.2.0 in the npm_and_yarn group across 1 directory
2026-06-16 08:10:39 -07:00
.github/workflows Improve supply chain configuration 2026-06-03 10:25:18 -04:00
decorator Add /decorator package.json for typescript compatibility 2022-05-02 14:37:04 -07:00
test use eslint 9 and update tmp package 2025-10-13 10:46:20 -07:00
.gitignore feat: initial implementation 2020-06-30 18:18:52 +01:00
.npmrc Improve supply chain configuration 2026-06-03 10:25:18 -04:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2020-07-20 16:45:01 +01:00
CODEOWNERS feat: initial implementation 2020-06-30 18:18:52 +01:00
CONTRIBUTING.md Create CONTRIBUTING.md 2020-07-20 16:43:42 +01:00
decorator.ts style: fix lint errors 2020-07-08 11:57:52 +01:00
eslint.config.mjs use eslint 9 and update tmp package 2025-10-13 10:46:20 -07:00
index.ts use eslint 9 and update tmp package 2025-10-13 10:46:20 -07:00
LICENSE Update LICENSE 2020-07-08 11:37:28 +02:00
package-lock.json build(deps-dev): bump js-yaml 2026-06-15 21:59:15 +00:00
package.json Improve supply chain configuration 2026-06-03 10:25:18 -04:00
README.md Update README.md 2020-07-20 16:47:59 +01:00
SECURITY.md Create SECURITY.md 2020-07-20 16:45:46 +01:00
tsconfig.json Fix tsconfig to use node moduleResolution with skipLibCheck 2025-10-07 20:24:09 +00:00
vitest.config.ts use eslint 9 and update tmp package 2025-10-13 10:46:20 -07:00

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) => unknown Provides a single value to use as the Key for the memoization. Defaults to JSON.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.