What is LRU Cache?
Keep recent items and evict the one that was used the longest time ago.
Type a tool name, format or task
Keep recent items and evict the one that was used the longest time ago. Interactive step-by-step animation with JavaScript code, complexity and front-end use cases.
Loading animation...
Keep recent items and evict the one that was used the longest time ago. On get, move the item to the newest end. On put, add the item at the newest end. If the cache is over capacity, remove the oldest item. Time complexity: O(1) per operation. Space complexity: O(capacity). Caching API responses, images and computed values in the browser without growing memory forever.
Keep recent items and evict the one that was used the longest time ago.
Time: O(1) per operation. Space: O(capacity).
Caching API responses, images and computed values in the browser without growing memory forever.
No. This converter runs in your browser for ordinary use, so the input is not sent to a backend by this tool.
Keep working without searching again.
An LRU (least recently used) cache keeps at most 3 items. When it is full, the item that has not been used for the longest time is thrown out.
class LRUCache {constructor(capacity) { this.cap = capacity; this.map = new Map(); }get(key) {if (!this.map.has(key)) return -1;const value = this.map.get(key);this.map.delete(key);this.map.set(key, value);return value;}put(key, value) {this.map.delete(key);this.map.set(key, value);if (this.map.size > this.cap) {this.map.delete(this.map.keys().next().value);}}}
Tip: use the arrow keys to step and the space bar to play or pause.