Hash Tables: Why Lookups Are Unreasonably Fast
Remember the foreshadowing in lesson one? Version B of “check the member list” used one line set.has(user)—list from 100 to 10 million people, time barely moved. We promised to unmask it in lesson five—today we take that “direct hit” magic apart: it doesn't scan; it computes.
Back to the organizing metaphor: searching a big drawer means flipping one by one because you don't know where things are. A hash table flips that completely—the moment you put something in, a fixed formula computes which bucket it belongs in; to look it up, run the same formula again and open that bucket. That formula is the hash function—a “locator formula”: no scanning, one step to where it is.
Below: 8 buckets numbered 0–7, and 6 names waiting to move in. Tap a name and watch three steps into a bucket: turn each character into its computer code and sum them, mod 8 (only 8 buckets), then fly into the computed bucket. Watch for: no “compare one by one” anywhere—the place is entirely computed.
You may have noticed: 阿芳 and 丽丽 both land in bucket 2! That's a hash collision—only 8 buckets, countless names; collisions are inevitable. The most common fix is charmingly simple: hang a mini linked list in the bucket; newcomers line up on the chain (jargon: “chaining”). Tap the three buttons in order—watch for how many peeks a lookup takes.
Now scale the data and race both lookup styles head-on. Pick a size, hit Race. Watch for the counter on the right: no matter how long the left side scans, it always stops at 1–2 peeks.
🗄 Walk the list (linear lookup)
Peeks 0🗃 Direct hit (hash lookup)
Peeks 0The hash table may be the structure serving you most each day—it just stays backstage. These four scenes all use the same move: “compute the place, one-step direct hit.”
Set & dictionaries
Lesson-one version B's Set, Python's dict, JS's Map—every “get by key” container in a language has a hash table inside.
Cache keys
A cache must answer “have we computed this?” in milliseconds—by hashing the question into a key for a direct lookup. That's next lesson's star.
Dedup
Deduping training corpora, crawlers asking “have we fetched this page?”—hash the content into a Set and check. Otherwise pairwise compares on billions of rows run until heat death.
session lookup
Every time you open ChatGPT, the server takes your session id and finds your conversation among tens of millions of online users instantly—not by walking a roster.
What this lesson wants to share
- Hash function = locator formula: put and find share one formula; the place is computed, not scanned
- Cost doesn't depend on data size: once for 6 people, once for 6 million—that's the truth behind version B's “direct hit”
- Collisions aren't scary: hang a mini chain in the bucket; if chains get long, add buckets and rehash (resize)
- Trade space for time: keep a “locator formula + buckets” ready for free lookups—in AI, Set, dictionaries, cache keys, dedup, and sessions are all this
- Review lens: when you see “scan a big list one by one” code, ask “why isn't this a hash?”