Tiem Cong Nghe

Showing suggestions.

Opening the store's index…
↑↓ to move↵ to openesc to close
Tiem Cong Nghe search
Animationv1.0.0~21 KB gzipped

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

FreeFree
Format

.zip · HTML / CSS / JS · v1.0.0

Free — no account, no email. Always the newest version.

Live preview

Version
Preview width
Zoom

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, #insertAfterHead and #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, set and delete
  • 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:

HTML
<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.

Ask about this product

Not sure it fits your project? Ask — the answer comes from this page.

Answers are written by AI from this page and can be wrong. The page itself is what counts.

Changelog

  1. 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.

  2. 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.

Your rating
Never shown. Only used if we need to reach you about your review.
0 / 2000

Your name and review are shown on this page; your email is not. See the terms.

  • 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