{"name":"p-memoize","version":"8.0.0","description":"Memoize promise-returning & async functions","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/p-memoize.git"},"funding":"https://github.com/sindresorhus/p-memoize?sponsor=1","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","types":"./dist/index.d.ts","exports":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"sideEffects":false,"engines":{"node":">=20"},"scripts":{"test":"xo && ava && npm run build && tsd","build":"del-cli dist && tsc","prepack":"npm run build"},"keywords":["promise","memoize","mem","memoization","function","cache","caching","optimize","performance","ttl","expire","async","await","promises","time","out","cancel","bluebird"],"dependencies":{"mimic-function":"^5.0.1","type-fest":"^4.41.0"},"devDependencies":{"@sindresorhus/tsconfig":"^8.0.1","@types/serialize-javascript":"^5.0.4","ava":"^6.4.1","del-cli":"^6.0.0","delay":"^6.0.0","p-defer":"^4.0.1","p-state":"^2.0.1","serialize-javascript":"^6.0.2","tsd":"^0.33.0","tsimp":"^2.0.12","xo":"^1.2.1"},"ava":{"environmentVariables":{"TSIMP_DIAG":"ignore"},"extensions":{"ts":"module"},"nodeArguments":["--import=tsimp/import"]},"xo":{"rules":{"@typescript-eslint/no-redundant-type-constituents":"off"}},"_id":"p-memoize@8.0.0","gitHead":"a006ac3224bbeb344cb62976e7f18bbc5ad61d14","bugs":{"url":"https://github.com/sindresorhus/p-memoize/issues"},"homepage":"https://github.com/sindresorhus/p-memoize#readme","_nodeVersion":"20.19.1","_npmVersion":"10.9.2","dist":{"integrity":"sha512-jdZ10MCxavHoIHwJ5oweOtYy6ElPixEHaMkz0AuaEMovR1MRpVvYFzIEHRxgMEpXYzNpRVByFAniAzwmd1/uug==","shasum":"1312d5a1ff5e902ef2d77b6ccadf00c67f127b6f","tarball":"http://registry-npm.ywwl.com/p-memoize/download/p-memoize-8.0.0.tgz","fileCount":5,"unpackedSize":18382,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIAvISn0RGKgvj5vspg2dyUuUKsvqL9XR5YqFDdtcEnS/AiEAtr5thaQq8YB7RmLWG2A9TkFEXeqxrDRmPP2HdYG4ZHQ="}],"size":5586,"noattachment":false,"key":"/p-memoize/-/p-memoize-8.0.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"},{"name":"fregante","email":"opensource@bfred.it"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/p-memoize_8.0.0_1755181896551_0.9086651723836916"},"_hasShrinkwrap":false,"readme":"# p-memoize\n\n> [Memoize](https://en.wikipedia.org/wiki/Memoization) promise-returning & async functions\n\nUseful for speeding up consecutive function calls by caching the result of calls with identical input.\n\n<!-- Please keep this section in sync with https://github.com/sindresorhus/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\nThis package is similar to [memoize](https://github.com/sindresorhus/memoize) 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 `memoize`](https://github.com/sindresorhus/memoize#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_ => 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\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##### shouldCache\n\nType: `(value, {key, argumentsList}) => boolean | Promise<boolean>`\n\nControls whether a fulfilled value should be written to the cache.\n\nIt runs after the function fulfills and before `cache.set`.\n\n- Omit to keep current behavior (always write).\n- Return `false` to skip writing to the cache (in-flight de-duplication is still cleared).\n- Throw or reject to propagate the error and skip caching.\n\n```js\nimport pMemoize from 'p-memoize';\n\n// Only cache defined values\nconst getMaybe = pMemoize(async key => db.get(key), {\n\tshouldCache: value => value !== undefined,\n});\n\n// Only cache non-empty arrays\nconst search = pMemoize(async query => fetchResults(query), {\n\tshouldCache: value => Array.isArray(value) && value.length > 0,\n});\n```\n\nNote: Affects only writes; reads from the cache are unchanged.\n\n### pMemoizeDecorator(options)\n\nReturns a decorator to memoize class methods (instance and static).\n\nNotes:\n\n- Only class methods are supported; regular functions are not part of the decorators proposals.\n- Requires the new ECMAScript decorators (TypeScript 5.0+). Legacy `experimentalDecorators` are not supported.\n- Babel’s legacy decorators are not supported as they implement a different proposal variant.\n- Private methods are not supported.\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\nThe decorator memoizes per-instance. You can clear the cache for an instance method using `pMemoizeClear(instance.method)`.\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) => 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 & async functions\n- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions\n- [More…](https://github.com/sindresorhus/promise-fun)\n","publish_time":1755181896745,"_cnpm_publish_time":1755181896745,"dist-tags":{"latest":"9.0.0","version4":"4.0.4"}}