Gathering detailed insights and metrics for lru.min
Gathering detailed insights and metrics for lru.min
π₯ An extremely fast and efficient LRU cache for JavaScript with high compatibility (including Browsers).
npm install lru.min
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.7
Supply Chain
99.5
Quality
82.2
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
21,537,433
Last Day
286,090
Last Week
1,493,084
Last Month
4,942,246
Last Year
21,537,433
8 Stars
45 Commits
1 Watching
2 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.1
Package Id
lru.min@1.1.1
Unpacked Size
32.76 kB
Size
8.64 kB
File Count
7
NPM Version
10.8.2
Node Version
20.17.0
Publised On
20 Sept 2024
Cumulative downloads
Total Downloads
Last day
-3.2%
286,090
Compared to previous day
Last week
2%
1,493,084
Compared to previous week
Last month
-3.4%
4,942,246
Compared to previous month
Last year
0%
21,537,433
Compared to previous year
π₯ An extremely fast and efficient LRU Cache for JavaScript (Browser compatible) β 6.8KB.
1# Node.js 2npm i lru.min
1# Bun 2bun add lru.min
1# Deno 2deno add npm:lru.min
1import { createLRU } from 'lru.min';
2
3const max = 2;
4const onEviction = (key, value) => {
5 console.log(`Key "${key}" with value "${value}" has been evicted.`);
6};
7
8const LRU = createLRU({
9 max,
10 onEviction,
11});
12
13LRU.set('A', 'My Value');
14LRU.set('B', 'Other Value');
15LRU.set('C', 'Another Value');
16
17// => Key "A" with value "My Value" has been evicted.
18
19LRU.has('B');
20LRU.get('B');
21LRU.delete('B');
22
23// => Key "B" with value "Other Value" has been evicted.
24
25LRU.peek('C');
26
27LRU.clear(); // LRU.evict(max)
28
29// => Key "C" with value "Another Value" has been evicted.
30
31LRU.set('D', "You're amazing π");
32
33LRU.size; // 1
34LRU.max; // 2
35LRU.available; // 1
36
37LRU.resize(10);
38
39LRU.size; // 1
40LRU.max; // 10
41LRU.available; // 9
For up-to-date documentation, always follow the README.md in the GitHub repository.
1import { createLRU } from 'lru.min';
1const { createLRU } = require('lru.min');
Requires ES6.
1<script src="https://cdn.jsdelivr.net/npm/lru.min@1.x.x/browser/lru.min.js"></script>
Set maximum size when creating LRU.
1const LRU = createLRU({ max: 150_000 });
Also, you can set a callback for every deletion/eviction:
1const LRU = createLRU({
2 max: 150_000,
3 onEviction: (key, value) => {
4 // do something
5 },
6});
Adds a key-value pair to the cache. Updates the value if the key already exists
1LRU.set('key', 'value');
undefined
keys will simply be ignored.
Retrieves the value for a given key and moves the key to the most recent position.
1LRU.get('key');
Retrieves the value for a given key without changing its position.
1LRU.peek('key');
1LRU.has('key');
1LRU.delete('key');
Evicts the specified number of the oldest items from the cache.
1LRU.evict(1000);
[!TIP]
- Methods that perform eviction(s) when maximum size is reached:
set
andresize
.- Methods that always perform eviction(s):
delete
,clear
, andevict
itself.
Resizes the cache to a new maximum size, evicting items if necessary.
1LRU.resize(50_000);
Clears and disposes (if used) all key-value pairs from the cache.
1LRU.clear();
1LRU.max;
1LRU.size;
1LRU.available;
Iterates over all keys in the cache, from most recent to least recent.
1const keys = [...LRU.keys()];
Iterates over all values in the cache, from most recent to least recent.
1const values = [...LRU.values()];
Iterates over [key, value]
pairs in the cache, from most recent to least recent.
1const entries = [...LRU.entries()];
Iterates over each value-key pair in the cache, from most recent to least recent.
1LRU.forEach((value, key) => {
2 // do something
3});
You can set types for both keys and values. For example:
1import { createLRU } from 'lru.min'; 2 3type Key = number; 4 5type Value = { 6 name: string; 7}; 8 9const LRU = createLRU<Key, Value>({ max: 1000 }); 10 11LRU.set(1, { name: 'Peter' }); 12LRU.set(2, { name: 'Mary' });
Also:
1import { createLRU, type CacheOptions } from 'lru.min'; 2 3type Key = number; 4 5type Value = { 6 name: string; 7}; 8 9const options: CacheOptions<Key, Value> = { 10 max: 10, 11 onEviction(key, value) { 12 console.log(key, value); 13 }, 14}; 15 16// No need to repeat the type params 17const LRU = createLRU(options); 18 19LRU.set(1, { name: 'Peter' }); 20LRU.set(2, { name: 'Mary' });
The benchmark is performed by comparing 1,000,000
runs through a maximum cache limit of 100,000
, getting 333,333
caches and deleting 200,000
keys 10 consecutive times, clearing the cache every run.
1# Time: 2 lru.min: 240.45ms 3 lru-cache: 258.32ms 4 quick-lru: 279.89ms 5 6# CPU: 7 lru.min: 275558.30Β΅s 8 lru-cache: 306858.30Β΅s 9 quick-lru: 401318.80Β΅s
Please check the SECURITY.md.
See the Contributing Guide and please follow our Code of Conduct π
lru.min is based and inspired on the architecture and code of both lru-cache and quick-lru, simplifying their core concepts for enhanced performance and compatibility.
For more comprehensive features such as TTL support, consider using and supporting them π€
Architecture's essence:
It's not the same code, but majority based on this.
1let free: number[] = []; 2 3const keyMap: Map<Key, number> = new Map(); 4const keyList: (Key | undefined)[] = new Array(max).fill(undefined); 5const valList: (Value | undefined)[] = new Array(max).fill(undefined); 6const next: number[] = new Array(max).fill(0); 7const prev: number[] = new Array(max).fill(0);
Name of methods and options (including their final functionality ideas):
resize
peek
onEviction
forEach
entriesDescending
as entries
lru.min is under the MIT License.
Copyright Β© 2024-present Weslley AraΓΊjo and lru.min contributors.
No vulnerabilities found.
No security vulnerabilities found.