{"name":"quick-lru","version":"7.2.0","keywords":["lru","quick","cache","caching","least","recently","used","fast","map","hash","buffer"],"author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"license":"MIT","_id":"quick-lru@7.2.0","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"homepage":"https://github.com/sindresorhus/quick-lru#readme","bugs":{"url":"https://github.com/sindresorhus/quick-lru/issues"},"nyc":{"reporter":["text","lcov"]},"dist":{"shasum":"242ef4b936afb0c2455e97962958d19c7e7cb11d","tarball":"http://registry-npm.ywwl.com/quick-lru/download/quick-lru-7.2.0.tgz","fileCount":5,"integrity":"sha512-fG4L8TlD1CacJiGMGPxM1/K8l/GaKL2eFQZ6DWAjxZYxSf07DkumbC/Mhh+u/NHvxkfQVL25By0pxBS8QE9ZrQ==","signatures":[{"sig":"MEQCIAYGyoa5XL5gE06qerawtiJ0qBbviGsWt0ISvWehRkvqAiBSIuh2cIPdkhVNHNFQpcBPs4+4bdJ0ei+pq4EDw4FIIw==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":20290,"size":6086,"noattachment":false,"key":"/quick-lru/-/quick-lru-7.2.0.tgz"},"type":"module","types":"./index.d.ts","engines":{"node":">=18"},"exports":{"types":"./index.d.ts","default":"./index.js"},"funding":"https://github.com/sponsors/sindresorhus","gitHead":"02786037f0662a5d97a544020aa4fbbc429990e8","scripts":{"test":"xo && nyc ava && tsd"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"repository":{"url":"git+https://github.com/sindresorhus/quick-lru.git","type":"git"},"_npmVersion":"10.9.2","description":"Simple “Least Recently Used” (LRU) cache","directories":{},"sideEffects":false,"_nodeVersion":"20.19.5","_hasShrinkwrap":false,"devDependencies":{"xo":"^0.56.0","ava":"^5.3.1","nyc":"^15.1.0","tsd":"^0.29.0"},"_npmOperationalInternal":{"tmp":"tmp/quick-lru_7.2.0_1758061145665_0.6742163866471944","host":"s3://npm-registry-packages-npm-production"},"readme":"# quick-lru [![Coverage Status](https://codecov.io/gh/sindresorhus/quick-lru/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/quick-lru/branch/main)\n\n> Simple [“Least Recently Used” (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29)\n\nUseful when you need to cache something and limit memory usage.\n\nSee the [algorithm section](#algorithm) for implementation details.\n\n## Install\n\n```sh\nnpm install quick-lru\n```\n\n## Usage\n\n```js\nimport QuickLRU from 'quick-lru';\n\nconst lru = new QuickLRU({maxSize: 1000});\n\nlru.set('🦄', '🌈');\n\nlru.has('🦄');\n//=> true\n\nlru.get('🦄');\n//=> '🌈'\n```\n\n## API\n\n### new QuickLRU(options?)\n\nReturns a new instance.\n\nIt's a [`Map`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map) subclass.\n\n### options\n\nType: `object`\n\n#### maxSize\n\n*Required*\\\nType: `number`\n\nThe target maximum number of items before evicting the least recently used items.\n\n> [!NOTE]\n> This package uses an [algorithm](#algorithm) which maintains between `maxSize` and `2 × maxSize` items for performance reasons. The cache may temporarily contain up to twice the specified size due to the dual-cache design that avoids expensive delete operations.\n\n#### maxAge\n\nType: `number`\\\nDefault: `Infinity`\n\nThe maximum number of milliseconds an item should remain in the cache.\nBy default, `maxAge` will be `Infinity`, which means that items will never expire.\n\nLazy expiration occurs upon the next `write` or `read` call.\n\nIndividual expiration of an item can be specified by the `set(key, value, options)` method.\n\n#### onEviction\n\n*Optional*\\\nType: `(key, value) => void`\n\nCalled right before an item is evicted from the cache due to LRU pressure, TTL expiration, or manual eviction via `evict()`.\n\nUseful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).\n\n> [!NOTE]\n> This callback is **not** called for manual removals via `delete()` or `clear()`. It fires for automatic evictions and manual evictions via `evict()`.\n\n### Instance\n\nThe instance is an [`Iterable`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols) of `[key, value]` pairs so you can use it directly in a [`for…of`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of) loop.\n\nBoth `key` and `value` can be of any type.\n\n#### .set(key, value, options?)\n\nSet an item. Returns the instance.\n\nIndividual expiration of an item can be specified with the `maxAge` option. If not specified, the global `maxAge` value will be used in case it is specified in the constructor; otherwise, the item will never expire.\n\n#### .get(key)\n\nGet an item.\n\n#### .has(key)\n\nCheck if an item exists.\n\n#### .peek(key)\n\nGet an item without marking it as recently used.\n\n#### .delete(key)\n\nDelete an item.\n\nReturns `true` if the item is removed or `false` if the item doesn't exist.\n\n#### .clear()\n\nDelete all items.\n\n#### .expiresIn(key)\n\nGet the remaining time to live (in milliseconds) for the given item, or `undefined` if the item is not in the cache.\n\n- Does not mark the item as recently used.\n- Does not trigger lazy expiration or remove the entry when it’s expired.\n- Returns `Infinity` if the item has no expiration (`maxAge` not set for the item and no global `maxAge`).\n- May return a negative number if the item has already expired but has not yet been lazily removed.\n\n#### .resize(maxSize)\n\nUpdate the `maxSize`, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee.\n\nUseful for on-the-fly tuning of cache sizes in live systems.\n\n#### .evict(count?)\n\nEvict the least recently used items from the cache.\n\nThe `count` parameter specifies how many items to evict. Defaults to 1.\n\nIt will always keep at least one item in the cache.\n\n```js\nimport QuickLRU from 'quick-lru';\n\nconst lru = new QuickLRU({maxSize: 10});\n\nlru.set('a', 1);\nlru.set('b', 2);\nlru.set('c', 3);\n\nlru.evict(2); // Evicts 'a' and 'b'\n\nconsole.log(lru.has('a'));\n//=> false\n\nconsole.log(lru.has('c'));\n//=> true\n```\n\n#### .keys()\n\nIterable for all the keys.\n\n#### .values()\n\nIterable for all the values.\n\n#### .entriesAscending()\n\nIterable for all entries, starting with the oldest (ascending in recency).\n\n#### .entriesDescending()\n\nIterable for all entries, starting with the newest (descending in recency).\n\n#### .entries()\n\nIterable for all entries, starting with the oldest (ascending in recency).\n\n**This method exists for `Map` compatibility. Prefer [.entriesAscending()](#entriesascending) instead.**\n\n#### .forEach(callbackFunction, thisArgument)\n\nLoop over entries calling the `callbackFunction` for each entry (ascending in recency).\n\n**This method exists for `Map` compatibility. Prefer [.entriesAscending()](#entriesascending) instead.**\n\n#### .size *(getter)*\n\nThe stored item count.\n\n#### .maxSize *(getter)*\n\nThe set max size.\n\n#### .maxAge *(getter)*\n\nThe set max age.\n\n## Algorithm\n\nThis library implements a variant of the [hashlru algorithm](https://github.com/dominictarr/hashlru#algorithm) using JavaScript's `Map` for broader key type support.\n\n### How it works\n\nThe algorithm uses a dual-cache approach with two `Map` objects:\n\n1. New cache - Stores recently accessed items\n2. Old cache - Stores less recently accessed items\n\nOn `set()` operations:\n- If the key exists in the new cache, update it\n- Otherwise, add the key-value pair to the new cache\n- When the new cache reaches `maxSize`, promote it to become the old cache and create a fresh new cache\n\nOn `get()` operations:\n- If the key is in the new cache, return it directly\n- If the key is in the old cache, move it to the new cache (promoting its recency)\n\n### Benefits\n\n- Performance: Avoids expensive `delete` operations that can cause performance issues in JavaScript engines\n- Simplicity: No complex linked list management required\n- Cache efficiency: Maintains LRU semantics while being much faster than traditional implementations\n\n### Trade-offs\n\n- Size variance: The cache can contain between `maxSize` and `2 × maxSize` items temporarily\n- Memory overhead: Uses up to twice the target memory compared to strict LRU implementations\n\n### When to use\n\nChoose this implementation when:\n- You need high-performance caching with many operations\n- You can tolerate temporary size variance for better performance\n- You want simple, reliable caching without complex data structures\n\nConsider alternatives when:\n- You need strict memory limits (exactly `maxSize` items)\n- Memory usage is more critical than performance\n\n## Related\n\n- [yocto-queue](https://github.com/sindresorhus/yocto-queue) - Tiny queue data structure\n","publish_time":1758061145897,"_cnpm_publish_time":1758061145897,"dist-tags":{"latest":"7.3.0"}}