{"id":13447223,"url":"https://github.com/lukeed/flru","last_synced_at":"2025-10-09T18:24:36.115Z","repository":{"id":57238912,"uuid":"163701932","full_name":"lukeed/flru","owner":"lukeed","description":"A tiny (215B) and fast Least Recently Used (LRU) cache","archived":false,"fork":false,"pushed_at":"2019-07-11T19:26:23.000Z","size":12,"stargazers_count":309,"open_issues_count":1,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-14T11:43:49.676Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/lukeed.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":null,"support":null}},"created_at":"2018-12-31T23:01:52.000Z","updated_at":"2024-04-12T19:27:08.000Z","dependencies_parsed_at":"2022-09-05T07:51:17.156Z","dependency_job_id":null,"html_url":"https://github.com/lukeed/flru","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fflru","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fflru/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fflru/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fflru/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukeed","download_url":"https://codeload.github.com/lukeed/flru/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244838084,"owners_count":20518782,"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-07-31T05:01:11.344Z","updated_at":"2025-10-09T18:24:31.066Z","avatar_url":"https://github.com/lukeed.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# flru [![Build Status](https://travis-ci.org/lukeed/flru.svg?branch=master)](https://travis-ci.org/lukeed/flru)\n\n\u003e A tiny (215B) and fast Least Recently Used (LRU) cache\n\nInternally, two caches are kept. This is because it's far more performant to swap (and maintain) dictionaries than it is to `delete`/purge keys on every read/write interaction. Because of this, `flru` will store `2n` items in memory, where `n` is the [`max`](#max) limit. In practice, this means that with `max=3` and items `(a, b, c)` already written, writing a `d` value ***will not*** automatically purge the `a` key. Instead, `a` _can_ be retrieved, which would move it to the \"active\" cache. It's only when this \"active\" half exceeds the `max` that the \"stale\" half is purged.\n\n\u003e See [Usage](#Usage) for a visual explanation~!\n\nThis implementation is optimized for all-around performance – reads, writes, updates, and evictions.\n\n\nThis module is available in three formats:\n\n* **ES Module**: `dist/flru.mjs`\n* **CommonJS**: `dist/flru.js`\n* **UMD**: `dist/flru.min.js`\n\n\n## Install\n\n```\n$ npm install --save flru\n```\n\n\n## Usage\n\n```js\n// Legend:\n//    S =\u003e the stale cache\n//    A =\u003e the active cache\n\nconst flru = require('flru');\n\nlet cache = flru(3); // A=[]        S=[]\n\ncache.set('a', 1);   // A=[a]       S=[]\ncache.set('b', 2);   // A=[a,b]     S=[]\ncache.set('b', 9);   // A=[a,b]     S=[]\ncache.set('c', 3);   // A=[a,b,c]   S=[]\n\ncache.has('a'); //=\u003e true\n\ncache.set('d', 4);   // A=[d]       S=[a,b,c]\ncache.get('a');      // A=[d,a]     S=[a,b,c]\ncache.set('e', 5);   // A=[d,a,e]   S=[a,b,c]\ncache.get('a');      // A=[d,a,e]   S=[a,b,c]\ncache.get('c');      // A=[c]       S=[d,a,e]\n\ncache.has('c'); //=\u003e true\ncache.has('b'); //=\u003e false\ncache.has('a'); //=\u003e true\n\ncache.clear();       // A=[]      S=[]\n```\n\n\n## API\n\n### flru(max)\nreturn `Object`\n\nInitialize a new `flru` cache instance.\n\n#### max\nRequired: `true`\u003cbr\u003e\nType: `Number`\u003cbr\u003e\nDefault: `1`\n\nThe maximum number of items to maintain – must be a positive, non-zero integer!\n\n\u003e **Important:** The default value is pointless and will result in excessive computation. It's there only to avoid memory leak!\n\n\n### flru.has(key)\nReturn: `Boolean`\n\nCheck if the cache has the given key.\n\n#### key\nType: `String`\n\nThe key name to check.\n\n\n### flru.get(key)\nReturn: `Mixed`\n\nGet the assigned value for a given key. Will return `undefined` if the cache has evicted `key` or never contained it.\n\n#### key\nType: `String`\n\nThe item's unique name / identifier.\n\n\n### flru.set(key, value)\nReturn: `undefined`\n\nPersist an item to the cache by a given `key` name.\n\n#### key\nType: `String`\n\nThe item's unique name / identifier.\n\n#### value\nType: `Mixed`\n\nThe item's value to be cached.\n\n\n### flru.clear(keepOld)\nReturn: `undefined`\n\nReset the cache(s) and counter.\n\n#### keepOld\nType: `Boolean`\u003cbr\u003e\nDefault: `false`\n\nWhen `true`, preserves the stale/outgoing cache.\n\n\u003e **Important:** This is used internally \u0026 generally should be ignored!\n\n\n## Benchmarks\n\nYou can find benchmarks in the [`bench`]() directory. They are setup to run one library at a time so that there's no cross-contamination of memory management or Node's runtime caching.\n\n* `set` – writing values into _new_ keys\n* `update` – updating values into _existing_ keys\n* `evict` – writing `2 * limit` keys to the cache, forcing eviction\n\n\u003e Results below are with Node v10.13.0\n\n```\n# set()\nflru       x 45,261 ops/sec ±1.63% (94 runs sampled)\nlru-cache  x 14,240 ops/sec ±5.70% (85 runs sampled)\ntmp-cache  x  8,229 ops/sec ±3.06% (83 runs sampled)\ntiny-lru   x 24,415 ops/sec ±2.48% (91 runs sampled)\n\n# get()\nflru       x 78,585 ops/sec ±1.70% (98 runs sampled)\nlru-cache  x 27,409 ops/sec ±2.64% (93 runs sampled)\ntmp-cache  x  6,229 ops/sec ±1.06% (87 runs sampled)\ntiny-lru   x 20,313 ops/sec ±2.01% (96 runs sampled)\n\n# has()\nflru       x  79,843 ops/sec ±1.35% (97 runs sampled)\nlru-cache  x  31,354 ops/sec ±2.87% (90 runs sampled)\ntmp-cache  x 813,828 ops/sec ±64.67% (95 runs sampled)\ntiny-lru   x 128,250 ops/sec ±3.73% (93 runs sampled)\n\n# update()\nflru       x 44,885 ops/sec ±1.86% (95 runs sampled)\nlru-cache  x 15,616 ops/sec ±2.46% (94 runs sampled)\ntmp-cache  x  8,529 ops/sec ±0.85% (87 runs sampled)\ntiny-lru   x 23,060 ops/sec ±2.72% (93 runs sampled)\n\n# evict()\nflru       x 8,258 ops/sec ±1.48% (88 runs sampled)\nlru-cache  x 1,492 ops/sec ±2.60% (77 runs sampled)\ntmp-cache  x   836 ops/sec ±0.59% (95 runs sampled)\ntiny-lru   x 2,626 ops/sec ±2.61% (81 runs sampled)\n```\n\n\n## Related\n\n- [tmp-cache](https://github.com/lukeed/tmp-cache) - Full-featured (but slower) alternative, supporting time-sensitive expirations.\n- [`tiny-lru`](https://github.com/avoidwork/tiny-lru) - Same as `tmp-cache` but significantly faster.\n\n\n## License\n\nMIT © [Luke Edwards](https://lukeed.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Fflru","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukeed%2Fflru","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Fflru/lists"}