|
|
2 weeks ago | |
|---|---|---|
| .. | ||
| browser | 2 weeks ago | |
| lib | 2 weeks ago | |
| LICENSE | 2 weeks ago | |
| README.md | 2 weeks ago | |
| package.json | 2 weeks ago | |
🔥 An extremely fast, efficient, and lightweight LRU Cache for JavaScript (Browser compatible).
# Node.js
npm i lru.min
# Bun
bun add lru.min
# Deno
deno add npm:lru.min
import { createLRU } from 'lru.min';
const max = 2;
const onEviction = (key, value) => {
console.log(`Key "${key}" with value "${value}" has been evicted.`);
};
const LRU = createLRU({
max,
onEviction,
});
LRU.set('A', 'My Value');
LRU.set('B', 'Other Value');
LRU.set('C', 'Another Value');
// => Key "A" with value "My Value" has been evicted.
LRU.has('B');
LRU.get('B');
LRU.delete('B');
// => Key "B" with value "Other Value" has been evicted.
LRU.peek('C');
LRU.clear(); // ← recommended | LRU.evict(max) → (slower alternative)
// => Key "C" with value "Another Value" has been evicted.
LRU.set('D', "You're amazing 💛");
LRU.size; // 1
LRU.max; // 2
LRU.available; // 1
LRU.resize(10);
LRU.size; // 1
LRU.max; // 10
LRU.available; // 9
For up-to-date documentation, always follow the README.md in the GitHub repository.
import { createLRU } from 'lru.min';
const { createLRU } = require('lru.min');
Requires ES6.
<script src="https://cdn.jsdelivr.net/npm/lru.min@1.x.x/browser/lru.min.js"></script>
Set maximum size when creating LRU.
const LRU = createLRU({ max: 150_000 });
Also, you can set a callback for every deletion/eviction:
const LRU = createLRU({
max: 150_000,
onEviction: (key, value) => {
// do something
},
});
Adds a key-value pair to the cache. Updates the value if the key already exists
LRU.set('key', 'value');
undefinedkeys will simply be ignored.
Retrieves the value for a given key and moves the key to the most recent position.
LRU.get('key');
Retrieves the value for a given key without changing its position.
LRU.peek('key');
LRU.has('key');
LRU.delete('key');
Evicts the specified number of the oldest items from the cache.
LRU.evict(1000);
[!TIP]
- Methods that perform eviction(s) when maximum size is reached:
setandresize.- Methods that always perform eviction(s):
delete,clear, andevictitself.
Resizes the cache to a new maximum size, evicting items if necessary.
LRU.resize(50_000);
Clears and disposes (if used) all key-value pairs from the cache.
LRU.clear();
onEviction: O(1).onEviction: O(entries).LRU.max;
LRU.size;
LRU.available;
Iterates over all keys in the cache, from most recent to least recent.
const keys = [...LRU.keys()];
Iterates over all values in the cache, from most recent to least recent.
const values = [...LRU.values()];
Iterates over [key, value] pairs in the cache, from most recent to least recent.
const entries = [...LRU.entries()];
Iterates over each value-key pair in the cache, from most recent to least recent.
LRU.forEach((value, key) => {
// do something
});
[!NOTE]
- We use
O(keys),O(values),O(entries), andO(newMax - max)to explicitly indicate what is being iterated over. In traditional complexity notation, this would be represented asO(n).
You can set types for both keys and values. For example:
import { createLRU } from 'lru.min';
type Key = number;
type Value = {
name: string;
};
const LRU = createLRU<Key, Value>({ max: 1000 });
LRU.set(1, { name: 'Peter' });
LRU.set(2, { name: 'Mary' });
Also:
import { createLRU, type CacheOptions } from 'lru.min';
type Key = number;
type Value = {
name: string;
};
const options: CacheOptions<Key, Value> = {
max: 10,
onEviction(key, value) {
console.log(key, value);
},
};
// No need to repeat the type params
const LRU = createLRU(options);
LRU.set(1, { name: 'Peter' });
LRU.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.
- lru-cache
v11.0.0
# Time:
lru.min: 240.45ms
lru-cache: 258.32ms
# CPU:
lru.min: 275558.30µs
lru-cache: 306858.30µs
Please check the SECURITY.md.
See the Contributing Guide and please follow our Code of Conduct 🚀
[!IMPORTANT]
No lru-cache or quick-lru code is used in lru.min. For more comprehensive features such as TTL support, consider using and supporting them 🤝
lru.min is under the MIT License.
Copyright © 2024-present Weslley Araújo and lru.min contributors.