{"id":13528382,"url":"https://github.com/sindresorhus/p-memoize","last_synced_at":"2025-05-15T05:07:54.882Z","repository":{"id":12317281,"uuid":"71544860","full_name":"sindresorhus/p-memoize","owner":"sindresorhus","description":"Memoize promise-returning \u0026 async functions","archived":false,"fork":false,"pushed_at":"2023-06-21T00:29:21.000Z","size":63,"stargazers_count":406,"open_issues_count":5,"forks_count":31,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-07T17:27:43.554Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/security.md","support":null,"governance":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2016-10-21T08:17:59.000Z","updated_at":"2025-04-29T20:10:43.000Z","dependencies_parsed_at":"2023-09-27T10:37:36.792Z","dependency_job_id":"347f7817-ac3a-40cf-a9fd-872817b64703","html_url":"https://github.com/sindresorhus/p-memoize","commit_stats":{"total_commits":50,"total_committers":14,"mean_commits":"3.5714285714285716","dds":"0.42000000000000004","last_synced_commit":"aa01febcd1104b2aad7e5fb05040befc8493daf1"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fp-memoize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fp-memoize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fp-memoize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fp-memoize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/p-memoize/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254239738,"owners_count":22037759,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:02:29.387Z","updated_at":"2025-05-15T05:07:49.860Z","avatar_url":"https://github.com/sindresorhus.png","language":"TypeScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["Packages","JavaScript","TypeScript","others","Convenience Utilities"],"sub_categories":["sindresorhus's many Promise utilities (\u003cb\u003e\u003ccode\u003e\u0026nbsp;\u0026nbsp;5146⭐\u003c/code\u003e\u003c/b\u003e \u003cb\u003e\u003ccode\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;138🍴\u003c/code\u003e\u003c/b\u003e [see notes](https://github.com/sindresorhus/promise-fun)))","sindresorhus's many Promise utilities ([see notes](https://github.com/sindresorhus/promise-fun))"],"readme":"# p-memoize\n\n\u003e [Memoize](https://en.wikipedia.org/wiki/Memoization) promise-returning \u0026 async functions\n\nUseful for speeding up consecutive function calls by caching the result of calls with identical input.\n\n\u003c!-- Please keep this section in sync with https://github.com/sindresorhus/mem/blob/main/readme.md --\u003e\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\nThis package is similar to [mem](https://github.com/sindresorhus/mem) but with async-specific enhancements; in particular, it allows for asynchronous caches and does not cache rejected promises.\n\n## Install\n\n```sh\nnpm install p-memoize\n```\n\n## Usage\n\n```js\nimport pMemoize from 'p-memoize';\nimport got from 'got';\n\nconst memoizedGot = pMemoize(got);\n\nawait memoizedGot('https://sindresorhus.com');\n\n// This call is cached\nawait memoizedGot('https://sindresorhus.com');\n```\n\n### Caching strategy\n\nSimilar to the [caching strategy for `mem`](https://github.com/sindresorhus/mem#options) with the following exceptions:\n\n- Promises returned from a memoized function are locally cached until resolving, when their value is added to `cache`. Special properties assigned to a returned promise will not be kept after resolution and every promise may need to resolve with a serializable object if caching results in a database.\n- `.get()`, `.has()` and `.set()` methods on `cache` can run asynchronously by returning a promise.\n- Instead of `.set()` being provided an object with the properties `value` and `maxAge`, it will only be provided `value` as the first argument. If you want to implement time-based expiry, consider [doing so in `cache`](#time-based-cache-expiration).\n\n## API\n\n### pMemoize(fn, options?)\n\nReturns a memoized version of the given function.\n\n#### fn\n\nType: `Function`\n\nPromise-returning or async function to be memoized.\n\n#### options\n\nType: `object`\n\n##### cacheKey\n\nType: `Function`\\\nDefault: `arguments_ =\u003e arguments_[0]`\\\nExample: `arguments_ =\u003e 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\nSee the [caching strategy](#caching-strategy) section for more information.\n\n##### cache\n\nType: `object | false`\\\nDefault: `new Map()`\n\nUse a different cache storage. Must implement the following methods: `.has(key)`, `.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. To disable caching so that only concurrent executions resolve with the same value, pass `false`.\n\nSee the [caching strategy](https://github.com/sindresorhus/mem#caching-strategy) section in the `mem` package for more information.\n\n### pMemoizeDecorator(options)\n\nReturns a [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.\n\nNotes:\n\n- Only class methods and getters/setters can be memoized, not regular functions (they aren't part of the proposal);\n- Only [TypeScript’s decorators](https://www.typescriptlang.org/docs/handbook/decorators.html#parameter-decorators) are supported, not [Babel’s](https://babeljs.io/docs/en/babel-plugin-proposal-decorators), which use a different version of the proposal;\n- Being an experimental feature, they need to be enabled with `--experimentalDecorators`; follow TypeScript’s docs.\n\n#### options\n\nType: `object`\n\nSame as options for `pMemoize()`.\n\n```ts\nimport {pMemoizeDecorator} from 'p-memoize';\n\nclass Example {\n\tindex = 0\n\n\t@pMemoizeDecorator()\n\tasync counter() {\n\t\treturn ++this.index;\n\t}\n}\n\nclass ExampleWithOptions {\n\tindex = 0\n\n\t@pMemoizeDecorator()\n\tasync counter() {\n\t\treturn ++this.index;\n\t}\n}\n```\n\n### pMemoizeClear(memoized)\n\nClear all cached data of a memoized function.\n\nIt will throw when given a non-memoized function.\n\n## Tips\n\n### Time-based cache expiration\n\n```js\nimport pMemoize from 'p-memoize';\nimport ExpiryMap from 'expiry-map';\nimport got from 'got';\n\nconst cache = new ExpiryMap(10000); // Cached values expire after 10 seconds\n\nconst memoizedGot = pMemoize(got, {cache});\n```\n\n### Caching promise rejections\n\n```js\nimport pMemoize from 'p-memoize';\nimport pReflect from 'p-reflect';\n\nconst memoizedGot = pMemoize(async (url, options) =\u003e pReflect(got(url, options)));\n\nawait memoizedGot('https://example.com');\n// {isFulfilled: true, isRejected: false, value: '...'}\n```\n\n## Related\n\n- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning \u0026 async functions\n- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning \u0026 async functions\n- [More…](https://github.com/sindresorhus/promise-fun)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fp-memoize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fp-memoize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fp-memoize/lists"}