{"_id":"memoize","_rev":"4665500","name":"memoize","description":"Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input","dist-tags":{"latest":"11.0.0"},"maintainers":[{"name":"sindresorhus","email":""}],"time":{"modified":"2026-04-10T22:38:37.000Z","created":"2011-05-05T14:50:18.269Z","11.0.0":"2026-03-19T08:21:06.926Z","10.2.0":"2025-10-24T15:18:28.819Z","10.1.0":"2025-02-19T10:18:40.555Z","10.0.0":"2023-11-14T15:46:54.475Z","0.1.1":"2011-08-23T09:16:11.620Z","0.1.0":"2011-08-23T08:03:57.298Z","0.0.3":"2011-05-05T15:41:19.446Z","0.0.2":"2011-05-05T15:03:55.184Z","0.0.1":"2011-05-05T14:50:18.269Z"},"users":{},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"repository":{"type":"git","url":"git+https://github.com/sindresorhus/memoize.git"},"versions":{"11.0.0":{"name":"memoize","version":"11.0.0","description":"Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/memoize.git"},"funding":"https://github.com/sindresorhus/memoize?sponsor=1","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./distribution/index.d.ts","default":"./distribution/index.js"},"sideEffects":false,"engines":{"node":">=22"},"scripts":{"test":"xo && ava && npm run build && tsd --typings distribution/index.d.ts","build":"del-cli distribution && tsc","prepack":"npm run build"},"keywords":["memoize","function","mem","memoization","cache","caching","optimize","performance","ttl","expire","promise"],"dependencies":{"mimic-function":"^5.0.1"},"devDependencies":{"@sindresorhus/tsconfig":"^8.1.0","@types/serialize-javascript":"^5.0.4","ava":"^7.0.0","del-cli":"^7.0.0","delay":"^7.0.0","serialize-javascript":"^7.0.4","tsd":"^0.33.0","tsimp":"^2.0.12","xo":"^2.0.2"},"ava":{"timeout":"1m","extensions":{"ts":"module"},"nodeArguments":["--import=tsimp"],"workerThreads":false,"environmentVariables":{"TSIMP_DIAG":"ignore"}},"xo":{"rules":{"@typescript-eslint/no-unsafe-call":"off","@typescript-eslint/no-unsafe-member-access":"off","@typescript-eslint/no-unsafe-return":"off","@typescript-eslint/no-unsafe-type-assertion":"off"}},"gitHead":"7821ca06700dd13909e7bc71a1193e40f2611297","_id":"memoize@11.0.0","bugs":{"url":"https://github.com/sindresorhus/memoize/issues"},"homepage":"https://github.com/sindresorhus/memoize#readme","_nodeVersion":"25.8.0","_npmVersion":"11.11.0","dist":{"shasum":"8472a09bb0457ccbe4e3034ace0fbeeb30a13f7d","size":6950,"noattachment":false,"key":"/memoize/-/memoize-11.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-11.0.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_11.0.0_1773908466783_0.46798211736934037"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2026-03-19T08:21:06.926Z","publish_time":1773908466926,"_source_registry_name":"default","_cnpm_publish_time":1773908466926},"10.2.0":{"name":"memoize","version":"10.2.0","description":"Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/memoize.git"},"funding":"https://github.com/sindresorhus/memoize?sponsor=1","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./distribution/index.d.ts","default":"./distribution/index.js"},"sideEffects":false,"engines":{"node":">=18"},"scripts":{"test":"xo && ava && npm run build && tsd --typings distribution/index.d.ts","build":"del-cli distribution && tsc","prepack":"npm run build"},"keywords":["memoize","function","mem","memoization","cache","caching","optimize","performance","ttl","expire","promise"],"dependencies":{"mimic-function":"^5.0.1"},"devDependencies":{"@sindresorhus/tsconfig":"^6.0.0","@types/serialize-javascript":"^5.0.4","ava":"^6.1.3","del-cli":"^5.1.0","delay":"^6.0.0","serialize-javascript":"^6.0.2","ts-node":"^10.9.2","tsd":"^0.31.1","xo":"^0.59.3"},"ava":{"timeout":"1m","extensions":{"ts":"module"},"nodeArguments":["--loader=ts-node/esm"],"workerThreads":false},"xo":{"rules":{"@typescript-eslint/no-unsafe-return":"off"}},"gitHead":"4c2b35fad5841cea20274b461fe2528fa40a692d","_id":"memoize@10.2.0","bugs":{"url":"https://github.com/sindresorhus/memoize/issues"},"homepage":"https://github.com/sindresorhus/memoize#readme","_nodeVersion":"24.9.0","_npmVersion":"11.6.1","dist":{"shasum":"593f8066b922b791390d05e278dbeff163dad956","size":6396,"noattachment":false,"key":"/memoize/-/memoize-10.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-10.2.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_10.2.0_1761319108609_0.36495026127893704"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-10-24T15:18:28.819Z","publish_time":1761319108819,"_source_registry_name":"default","_cnpm_publish_time":1761319108819},"10.1.0":{"name":"memoize","version":"10.1.0","description":"Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/memoize.git"},"funding":"https://github.com/sindresorhus/memoize?sponsor=1","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./distribution/index.d.ts","default":"./distribution/index.js"},"sideEffects":false,"engines":{"node":">=18"},"scripts":{"test":"xo && ava && npm run build && tsd --typings distribution/index.d.ts","build":"del-cli distribution && tsc","prepack":"npm run build"},"keywords":["memoize","function","mem","memoization","cache","caching","optimize","performance","ttl","expire","promise"],"dependencies":{"mimic-function":"^5.0.1"},"devDependencies":{"@sindresorhus/tsconfig":"^6.0.0","@types/serialize-javascript":"^5.0.4","ava":"^6.1.3","del-cli":"^5.1.0","delay":"^6.0.0","serialize-javascript":"^6.0.2","ts-node":"^10.9.2","tsd":"^0.31.1","xo":"^0.59.3"},"ava":{"timeout":"1m","extensions":{"ts":"module"},"nodeArguments":["--loader=ts-node/esm"],"workerThreads":false},"xo":{"rules":{"@typescript-eslint/no-unsafe-return":"off"}},"_id":"memoize@10.1.0","gitHead":"e96965cf6ffc9af784bdebab6eef408d017a6620","bugs":{"url":"https://github.com/sindresorhus/memoize/issues"},"homepage":"https://github.com/sindresorhus/memoize#readme","_nodeVersion":"23.6.1","_npmVersion":"10.9.2","dist":{"shasum":"32a9d09da985a1ab518dfe9fd52d14d1d130446f","size":5776,"noattachment":false,"key":"/memoize/-/memoize-10.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-10.1.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_10.1.0_1739960320380_0.03932131984779752"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-02-19T10:18:40.555Z","publish_time":1739960320555,"_source_registry_name":"default","_cnpm_publish_time":1739960320555},"10.0.0":{"name":"memoize","version":"10.0.0","description":"Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/memoize.git"},"funding":"https://github.com/sindresorhus/memoize?sponsor=1","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"sideEffects":false,"engines":{"node":">=18"},"scripts":{"test":"xo && ava && npm run build && tsd --typings dist/index.d.ts","build":"del-cli dist && tsc","prepack":"npm run build"},"keywords":["memoize","function","mem","memoization","cache","caching","optimize","performance","ttl","expire","promise"],"dependencies":{"mimic-function":"^5.0.0"},"devDependencies":{"@sindresorhus/tsconfig":"^5.0.0","@types/serialize-javascript":"^5.0.4","ava":"^5.3.1","del-cli":"^5.1.0","delay":"^6.0.0","serialize-javascript":"^6.0.1","ts-node":"^10.9.1","tsd":"^0.29.0","xo":"^0.56.0"},"ava":{"timeout":"1m","extensions":{"ts":"module"},"nodeArguments":["--loader=ts-node/esm"]},"xo":{"rules":{"@typescript-eslint/no-unsafe-return":"off"}},"gitHead":"2a8997ededd818a07edab1a750cf969ba473de3c","bugs":{"url":"https://github.com/sindresorhus/memoize/issues"},"homepage":"https://github.com/sindresorhus/memoize#readme","_id":"memoize@10.0.0","_nodeVersion":"18.18.2","_npmVersion":"9.2.0","dist":{"shasum":"43fa66b2022363c7c50cf5dfab732a808a3d7147","size":5632,"noattachment":false,"key":"/memoize/-/memoize-10.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-10.0.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/memoize_10.0.0_1699976814316_0.694652217839381"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-11-14T15:46:54.475Z","publish_time":1699976814475,"_source_registry_name":"default","_cnpm_publish_time":1699976814475},"0.1.1":{"name":"memoize","description":"memoize caches your callbacks given a set of arguments w/ persistence","keywords":["memoize","cache"],"version":"0.1.1","author":{"name":"George Stagas","email":"gstagas@gmail.com"},"engines":{"node":"*"},"dependencies":{"tosource":">=0.1.1"},"main":"./memoize","repository":{"type":"git","url":"git://github.com/stagas/memoize.git"},"bugs":{"url":"https://github.com/stagas/memoize/issues"},"_id":"memoize@0.1.1","_engineSupported":true,"_npmVersion":"0.3.18","_nodeVersion":"v0.4.8","directories":{},"files":[""],"_defaultsLoaded":true,"dist":{"shasum":"d265a3458be5ce3bf254998b30a995ab91668a24","size":3803,"noattachment":false,"key":"/memoize/-/memoize-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-0.1.1.tgz"},"maintainers":[{"name":"sindresorhus","email":""}],"publish_time":1314090971620,"_hasShrinkwrap":false,"_cnpm_publish_time":1314090971620,"_cnpmcore_publish_time":"2021-12-17T14:30:16.595Z"},"0.1.0":{"name":"memoize","description":"memoize caches your callbacks given a set of arguments w/ persistence","keywords":["memoize","cache"],"version":"0.1.0","author":{"name":"George Stagas","email":"gstagas@gmail.com"},"engines":{"node":"*"},"dependencies":{"tosource":">=0.1.1"},"main":"./memoize","repository":{"type":"git","url":"git://github.com/stagas/memoize.git"},"bugs":{"url":"https://github.com/stagas/memoize/issues"},"_id":"memoize@0.1.0","_engineSupported":true,"_npmVersion":"0.3.18","_nodeVersion":"v0.4.8","directories":{},"files":[""],"_defaultsLoaded":true,"dist":{"shasum":"61c1037a0ca916ff4d33cd84db554d4988b96925","size":3746,"noattachment":false,"key":"/memoize/-/memoize-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-0.1.0.tgz"},"maintainers":[{"name":"sindresorhus","email":""}],"publish_time":1314086637298,"_hasShrinkwrap":false,"_cnpm_publish_time":1314086637298,"_cnpmcore_publish_time":"2021-12-17T14:30:16.841Z"},"0.0.3":{"name":"memoize","description":"memoize caches your callbacks given a set of arguments","keywords":["memoize","cache"],"version":"0.0.3","author":{"name":"George Stagas","email":"gstagas@gmail.com"},"engines":{"node":"*"},"dependencies":{"tosource":">=0.1.1"},"main":"./memoize","repository":{"type":"git","url":"git://github.com/stagas/memoize.git"},"bugs":{"url":"https://github.com/stagas/memoize/issues"},"_id":"memoize@0.0.3","_engineSupported":true,"_npmVersion":"0.3.18","_nodeVersion":"v0.4.7","directories":{},"files":[""],"_defaultsLoaded":true,"dist":{"shasum":"9a7a12ac9b719f7d307fb0882f191664f1ae6a23","size":3427,"noattachment":false,"key":"/memoize/-/memoize-0.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-0.0.3.tgz"},"publish_time":1304610079446,"maintainers":[{"name":"sindresorhus","email":""}],"_hasShrinkwrap":false,"_cnpm_publish_time":1304610079446,"_cnpmcore_publish_time":"2021-12-17T14:30:17.076Z"},"0.0.2":{"name":"memoize","description":"memoize caches your callbacks given a set of arguments","keywords":["memoize","cache"],"version":"0.0.2","author":{"name":"George Stagas","email":"gstagas@gmail.com"},"engines":{"node":"*"},"main":"./memoize","repository":{"type":"git","url":"git://github.com/stagas/memoize.git"},"bugs":{"url":"https://github.com/stagas/memoize/issues"},"_id":"memoize@0.0.2","_engineSupported":true,"_npmVersion":"0.3.18","_nodeVersion":"v0.4.7","directories":{},"files":[""],"_defaultsLoaded":true,"dist":{"shasum":"1fec54a625c3b753ea940630cdf33f9b95e3a103","size":3333,"noattachment":false,"key":"/memoize/-/memoize-0.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-0.0.2.tgz"},"publish_time":1304607835184,"maintainers":[{"name":"sindresorhus","email":""}],"_hasShrinkwrap":false,"_cnpm_publish_time":1304607835184,"_cnpmcore_publish_time":"2021-12-17T14:30:17.339Z"},"0.0.1":{"name":"memoize","description":"memoize caches your callbacks given a set of arguments","keywords":["memoize","cache"],"version":"0.0.1","author":{"name":"George Stagas","email":"gstagas@gmail.com"},"engines":{"node":"*"},"main":"./memoize","repository":{"type":"git","url":"git://github.com/stagas/memoize.git"},"bugs":{"url":"https://github.com/stagas/memoize/issues"},"_id":"memoize@0.0.1","_engineSupported":true,"_npmVersion":"0.3.18","_nodeVersion":"v0.4.7","directories":{},"files":[""],"_defaultsLoaded":true,"dist":{"shasum":"a58167513a0ce451a33db3684ccc27df1760df78","size":3319,"noattachment":false,"key":"/memoize/-/memoize-0.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/memoize/download/memoize-0.0.1.tgz"},"publish_time":1304607018269,"maintainers":[{"name":"sindresorhus","email":""}],"_hasShrinkwrap":false,"_cnpm_publish_time":1304607018269,"_cnpmcore_publish_time":"2021-12-17T14:30:17.516Z"}},"readme":"# memoize\n\n> [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input\n\nMemory is automatically released when an item expires or the cache is cleared.\n\n<!-- Please keep this section in sync with https://github.com/sindresorhus/p-memoize/blob/main/readme.md -->\n\nBy default, **only the memoized function's first argument is considered** via strict equality comparison. If you need to cache multiple arguments or cache `object`s *by value*, have a look at alternative [caching strategies](#caching-strategy) below.\n\nIf you want to memoize Promise-returning functions (like `async` functions), you might be better served by [p-memoize](https://github.com/sindresorhus/p-memoize).\n\n## Install\n\n```sh\nnpm install memoize\n```\n\n## Usage\n\n```js\nimport memoize from 'memoize';\n\nlet index = 0;\nconst counter = () => ++index;\nconst memoized = memoize(counter);\n\nmemoized('foo');\n//=> 1\n\n// Cached as it's the same argument\nmemoized('foo');\n//=> 1\n\n// Not cached anymore as the argument changed\nmemoized('bar');\n//=> 2\n\nmemoized('bar');\n//=> 2\n\n// Only the first argument is considered by default\nmemoized('bar', 'foo');\n//=> 2\n```\n\n##### Works well with Promise-returning functions\n\nBut you might want to use [p-memoize](https://github.com/sindresorhus/p-memoize) for more Promise-specific behaviors.\n\n```js\nimport memoize from 'memoize';\n\nlet index = 0;\nconst counter = async () => ++index;\nconst memoized = memoize(counter);\n\nconsole.log(await memoized());\n//=> 1\n\n// The return value didn't increase as it's cached\nconsole.log(await memoized());\n//=> 1\n```\n\n```js\nimport memoize from 'memoize';\nimport got from 'got';\nimport delay from 'delay';\n\nconst memoizedGot = memoize(got, {maxAge: 1000});\n\nawait memoizedGot('https://sindresorhus.com');\n\n// This call is cached\nawait memoizedGot('https://sindresorhus.com');\n\nawait delay(2000);\n\n// This call is not cached as the cache has expired\nawait memoizedGot('https://sindresorhus.com');\n```\n\n### Caching strategy\n\nBy default, only the first argument is compared via exact equality (`===`) to determine whether a call is identical.\n\n```js\nimport memoize from 'memoize';\n\nconst pow = memoize((a, b) => Math.pow(a, b));\n\npow(2, 2); // => 4, stored in cache with the key 2 (number)\npow(2, 3); // => 4, retrieved from cache at key 2 (number), it's wrong\n```\n\nYou will have to use the `cache` and `cacheKey` options appropriate to your function. In this specific case, the following could work:\n\n```js\nimport memoize from 'memoize';\n\nconst pow = memoize((a, b) => Math.pow(a, b), {\n\tcacheKey: arguments_ => arguments_.join(',')\n});\n\npow(2, 2); // => 4, stored in cache with the key '2,2' (both arguments as one string)\npow(2, 3); // => 8, stored in cache with the key '2,3'\n```\n\nMore advanced examples follow.\n\n#### Example: Options-like argument\n\nIf your function accepts an object, it won't be memoized out of the box:\n\n```js\nimport memoize from 'memoize';\n\nconst heavyMemoizedOperation = memoize(heavyOperation);\n\nheavyMemoizedOperation({full: true}); // Stored in cache with the object as key\nheavyMemoizedOperation({full: true}); // Stored in cache with the object as key, again\n// The objects appear the same, but in JavaScript, they're different objects\n```\n\nYou might want to serialize or hash them, for example using `JSON.stringify` or something like [serialize-javascript](https://github.com/yahoo/serialize-javascript), which can also serialize `RegExp`, `Date` and so on.\n\n```js\nimport memoize from 'memoize';\n\nconst heavyMemoizedOperation = memoize(heavyOperation, {cacheKey: JSON.stringify});\n\nheavyMemoizedOperation({full: true}); // Stored in cache with the key '[{\"full\":true}]' (string)\nheavyMemoizedOperation({full: true}); // Retrieved from cache\n```\n\nThe same solution also works if it accepts multiple serializable objects:\n\n```js\nimport memoize from 'memoize';\n\nconst heavyMemoizedOperation = memoize(heavyOperation, {cacheKey: JSON.stringify});\n\nheavyMemoizedOperation('hello', {full: true}); // Stored in cache with the key '[\"hello\",{\"full\":true}]' (string)\nheavyMemoizedOperation('hello', {full: true}); // Retrieved from cache\n```\n\n#### Example: Multiple non-serializable arguments\n\nIf your function accepts multiple arguments that aren't supported by `JSON.stringify` (e.g. DOM elements and functions), you can instead extend the initial exact equality (`===`) to work on multiple arguments using [`many-keys-map`](https://github.com/fregante/many-keys-map):\n\n```js\nimport memoize from 'memoize';\nimport ManyKeysMap from 'many-keys-map';\n\nconst addListener = (emitter, eventName, listener) => emitter.on(eventName, listener);\n\nconst addOneListener = memoize(addListener, {\n\tcacheKey: arguments_ => arguments_, // Use *all* the arguments as key\n\tcache: new ManyKeysMap() // Correctly handles all the arguments for exact equality\n});\n\naddOneListener(header, 'click', console.log); // `addListener` is run, and it's cached with the `arguments` array as key\naddOneListener(header, 'click', console.log); // `addListener` is not run again because the arguments are the same\naddOneListener(mainContent, 'load', console.log); // `addListener` is run, and it's cached with the `arguments` array as key\n```\n\nBetter yet, if your function’s arguments are compatible with `WeakMap`, you should use [`deep-weak-map`](https://github.com/futpib/deep-weak-map) instead of `many-keys-map`. This will help avoid memory leaks.\n\n## API\n\n### memoize(fn, options?)\n\n#### fn\n\nType: `Function`\n\nThe function to be memoized.\n\n#### options\n\nType: `object`\n\n##### maxAge\n\nType: `number` | `Function`\\\nDefault: `Infinity`\\\nExample: `arguments_ => arguments_ < new Date() ? Infinity : 60_000`\n\nMilliseconds until the cache entry expires.\n\nIf a function is provided, it receives the arguments and must return the max age.\n\n- `0` or negative values: Do not cache the result\n- `Infinity`: Cache indefinitely (no expiration)\n- Positive finite number: Cache for the specified milliseconds\n\n##### cacheKey\n\nType: `Function`\\\nDefault: `arguments_ => arguments_[0]`\\\nExample: `arguments_ => JSON.stringify(arguments_)`\n\nDetermines the cache key for storing the result based on the function arguments. By default, **only the first argument is considered**.\n\nA `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option).\n\nRefer to the [caching strategies](#caching-strategy) section for more information.\n\n##### cache\n\nType: `object`\\\nDefault: `new Map()`\n\nUse a different cache storage. Must implement the following methods: `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache.\n\nRefer to the [caching strategies](#caching-strategy) section for more information.\n\n### memoizeDecorator(options)\n\nReturns a [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods, static class methods, or getters.\n\nNotes:\n\n- Uses [TC39 stage 3 decorators](https://github.com/tc39/proposal-decorators), supported natively in TypeScript 5.0+ and modern JavaScript engines;\n- Only class methods and getters can be memoized.\n\n#### options\n\nType: `object`\n\nSame as options for `memoize()`.\n\n```ts\nimport {memoizeDecorator} from 'memoize';\n\nclass Example {\n\tindex = 0\n\n\t@memoizeDecorator()\n\tcounter() {\n\t\treturn ++this.index;\n\t}\n}\n\nclass ExampleWithOptions {\n\tindex = 0\n\n\t@memoizeDecorator({maxAge: 1000})\n\tcounter() {\n\t\treturn ++this.index;\n\t}\n}\n\nclass ExampleWithGetter {\n\t@memoizeDecorator()\n\tget value() {\n\t\treturn expensiveComputation();\n\t}\n}\n```\n\n### memoizeClear(fn)\n\nClear all cached data of a memoized function.\n\n#### fn\n\nType: `Function`\n\nThe memoized function.\n\n### memoizeIsCached(fn, ...arguments)\n\nCheck if a specific set of arguments is cached for a memoized function.\n\nReturns `true` if the arguments are cached and not expired, `false` otherwise.\n\nUses the same argument processing as the memoized function, including any custom `cacheKey` function.\n\n```js\nimport memoize, {memoizeIsCached} from 'memoize';\n\nconst expensive = memoize((a, b) => a + b, {cacheKey: JSON.stringify});\nexpensive(1, 2);\n\nmemoizeIsCached(expensive, 1, 2);\n//=> true\n\nmemoizeIsCached(expensive, 3, 4);\n//=> false\n```\n\n#### fn\n\nType: `Function`\n\nThe memoized function.\n\n#### arguments\n\nThe arguments to check.\n\n## Tips\n\n### Cache statistics\n\nIf you want to know how many times your cache had a hit or a miss, you can make use of [stats-map](https://github.com/SamVerschueren/stats-map) as a replacement for the default cache.\n\n#### Example\n\n```js\nimport memoize from 'memoize';\nimport StatsMap from 'stats-map';\nimport got from 'got';\n\nconst cache = new StatsMap();\nconst memoizedGot = memoize(got, {cache});\n\nawait memoizedGot('https://sindresorhus.com');\nawait memoizedGot('https://sindresorhus.com');\nawait memoizedGot('https://sindresorhus.com');\n\nconsole.log(cache.stats);\n//=> {hits: 2, misses: 1}\n```\n\n## Related\n\n- [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions\n","_attachments":{},"homepage":"https://github.com/sindresorhus/memoize#readme","bugs":{"url":"https://github.com/sindresorhus/memoize/issues"},"license":"MIT"}