LRU Cache Animation
A hash map beside a doubly linked list: hits fly to the front, the oldest node falls away. The O(1) LRU cache, ready for your lessons, slides and posts
.zip · HTML / CSS / JS · v1.0.0
Free — no account, no email. Always the newest version.
Live preview
About
An LRU cache is easiest to understand when you watch it forget. This animation runs the real
get and put from the free algorithm page, line by line: a hash map finds the node for a key, a
doubly linked list keeps the nodes from most to least recently used, and every pointer is
rewired one assignment at a time. A hit takes its node out of the list and flies it back to the
front; when the cache is full, the node before tail falls away and the map forgets its key. The
line of code that is running lights up beside it, and counters and a hit-rate chart show what the
policy bought.
- The listing's own
get,put,#moveToFront,#unlink,#insertAfterHeadand#evictLeastRecent, with the call stack above them - Next and prev pointers drawn as arrows, the one just assigned lit, and the stale ones dimmed while a node is held in the air
- The hash map beside the list, with a line from each key to its node and a probe box for every
get,setanddelete - Three seeded scenarios, browser tabs, thrashing and a big scan, or your own operations on a capacity from 0 to 6
- A script strip of the operations, the running one lit and the finished ones coloured by how they ended
- Hits, misses, evictions, the hit rate, and what the same list would score on other capacities
- A full player with a scrubber and 0.5×–4× speed, built to look good on a slide or in a recording
- Every colour, radius, font and timing is a CSS custom property; light and dark follow your page
- Nothing to install: one CSS file and one script, or one React component
Features · 12
- Animates get and put exactly as the free algorithm page writes them: a hash map from key to node, a doubly linked list between head and tail sentinels, and the helpers #moveToFront, #unlink, #insertAfterHead and #evictLeastRecent, line for line
- A hit takes its node out of the list and flies it back to the front, a miss flashes the lookup, a new key builds a node in the air and links it in, and at capacity the node before tail falls away and its map entry is cleared
- Every pointer assignment gets its own frame: next arrows in cyan, prev arrows in pink, the one just assigned lit, and the pointers still waiting to be fixed dimmed while a node is held in the air
- The map is drawn beside the list: one row per slot, a line from each key to its node, and a probe box that shows what the map was asked (get, set or delete) and what it answered
- The listing's own variables (node, existing, leastRecent) tag the node that holds them, and the cards warm up the more recently they were used
- Counters for hits, misses and evictions, the live hit rate, one square per read, and bars for the hit rate the same list would have had on a capacity of 1 to 6
- Three seeded scenarios (browser tabs, thrashing, a big scan), or type your own operations like put 1 10; get 1 on a capacity from 0 to 6
- A script strip lists the operations: the one running is lit, and each finished one is coloured by how it ended (hit, miss, update or eviction)
- The code panel shows the method that is running, the constructor, get, put or a helper, with the call stack above it
- Player: play, pause, step back and forward, reset, scrub, 0.5×–4× speed, optional loop
- Plain HTML/CSS/JS and React (Next.js ready) builds, light and dark
- Zero dependencies, about 21 KB gzipped (script and CSS together)
How to use it
Link the stylesheet and the script once, then add an empty element with
data-lru-cache-animation wherever you want a player:
<link rel="stylesheet" href="lru-cache-animation.css" />
<script src="lru-cache-animation.js" defer></script>
<div class="lru-cache-animation" data-lru-cache-animation></div>
<div
class="lru-cache-animation"
data-lru-cache-animation
data-lru-cache-animation-scenario="thrashing"
data-lru-cache-animation-capacity="3"
></div>
<div
class="lru-cache-animation"
data-lru-cache-animation
data-lru-cache-animation-operations="put 1 10; put 2 20; get 1; put 3 30; get 2"
data-lru-cache-animation-capacity="2"
></div>Options as data attributes, all starting with data-lru-cache-animation-:
| Attribute | Default | What it does |
|---|---|---|
-scenario |
browser-tabs |
browser-tabs, thrashing or scan: the list of operations to run |
-operations |
the scenario's | Your own list: get key and put key value, separated by ;, , or new lines (up to 30, numbers from -99 to 999) |
-capacity |
the scenario's | How many entries the cache holds (0–6) |
-seed |
7 |
Which list the scenario builds: the same seed always gives the same one |
-speed |
1 |
0.25 to 4 |
-autoplay |
true |
Plays on its own once loaded (never with reduced motion) |
-loop |
false |
Starts again after a pause on the summary |
-code |
true |
false hides the code panel |
-inputs |
true |
false hides the rows of inputs (scenario, operations, capacity) |
Elements added after the page loads: LruCacheAnimation.init(container); it returns a
function that undoes it. For full control, LruCacheAnimation.createLruCacheAnimation(element, options)
returns a handle with setScenario, setOperations(text) (it returns false and says why on the
page when the list cannot be read), setCapacity, shuffle, player (play,
pause, seek…), trace() and destroy.
Copy LruCacheAnimation.tsx, lru-cache-animation.css and the core folder into your
project:
import { LruCacheAnimation } from "./LruCacheAnimation";
<LruCacheAnimation />
<LruCacheAnimation scenario="thrashing" capacity={3} autoplay={false} />
<LruCacheAnimation operations="put 1 10; put 2 20; get 1" capacity={2} loop inputs={false} onFrame={(frame) => console.log(frame.caption)} />The props are the options above (scenario, operations, capacity, seed, speed,
autoplay, loop, code, inputs), plus onFrame(frame, index), called with every
frame drawn. Changing a prop starts the list again. Every other prop goes to the root <div>
(className, id, aria-*…). React 18 or 19.
The file starts with "use client", so use it from any page or server component. With the App
Router nothing else is needed. With the Pages Router, move the CSS import into pages/_app.
Set these on .lru-cache-animation (or a class of your own):
| Property | What it changes |
|---|---|
--lru-tone-0 … --lru-tone-7 |
The eight key colours (cyan, violet, pink, amber, mint, sky, orange, lime); a key keeps its colour in the map, on its card and in the order strip |
--lru-next, --lru-prev |
The arrows of the next and the prev pointers |
--lru-lit, --lru-hop |
The node being worked on, and what was just assigned or is held in the air |
--lru-orb |
The ink every card is mixed with |
--lru-bg, --lru-surface, --lru-surface-strong, --lru-blur |
The background and the frosted glass |
--lru-edge, --lru-shadow, --lru-code-bg |
Borders, shadows, the code panel |
--lru-text, --lru-muted, --lru-on-accent, --lru-error, --lru-ring |
Text, secondary text, text on the coloured tags and buttons, the error message, focus ring |
--lru-font, --lru-mono |
The fonts for words and for code and numbers |
--lru-radius |
Corners of the player |
--lru-duration, --lru-ease |
How fast and how softly cards glide and fall |
Colours use light-dark(): they follow color-scheme on your page. With
prefers-reduced-motion, nothing plays on its own and frames change instantly; every control
still works.
Ask about this product
Not sure it fits your project? Ask — the answer comes from this page.
Changelog
1.0.0First releaseLatest
Plain HTML/CSS/JS and React builds of a step-by-step LRU cache animation: a hash map beside a doubly linked list with sentinels, nodes that fly to the front on a hit, every pointer rewired one assignment at a time, the least recent node falling away on eviction, live code, hit, miss and eviction counters with a hit rate by capacity, three seeded scenarios or your own operations, player controls, light and dark.
- Fixes and new features land behind the same download, which always serves the latest version.
Reviews
No reviews yet
Used it? Be the first to say how it went.
Write a review
How did it work for you? Reviews appear once we have read them.
You might also like
- AnimationData structures
Trie Animation
A glowing tree that grows letter by letter, with shared prefixes, snapping lookups and autocomplete: a Trie ready for your lessons, slides and posts
Free - AnimationData structures
Union-Find Animation
A glowing forest where groups merge and paths flatten: Union-Find with union by size and path compression, ready for your lessons, slides and posts
Free



