What is a hash table — and why a Set found in 3 ms what an array took 2 seconds to find

You have a list of 100,000 email addresses. For each of 100,000 new sign-ups you need to check: is this address already there?
The AI wrote this:
for (const email of applications) {
if (emails.includes(email)) { /* ... */ }
}
We timed it in Node 22: about 2 seconds. Then we changed one line — put the addresses into a Set and asked set.has(email). The same 100,000 checks took 3 milliseconds. Hundreds of times faster.
Here's the trick. includes walks the array from start to end. A Set is built as a hash table on the inside — and it doesn't walk anything. Let's see how that's possible.
What a hash table is
A hash table is a way to store "key → value" pairs so you can jump straight from a key to its value instead of searching for it.
Picture a coat check. You hand over your jacket and get ticket 47. When you come back, the attendant doesn't walk past every hanger looking at jackets. They go straight to hook 47.
A hash table does the same thing. Except it doesn't hand you a ticket — it computes the ticket from the key itself.
How it works
Inside there's a plain row of slots, like hangers in a coat check. When you store something by key, three steps happen:
- The key goes through a hash function. That's a formula that turns any string into a number.
"[email protected]"comes out as, say,839201. - The number becomes a slot index. For example, take the remainder after dividing by the number of slots. You get 17.
- The value goes into slot 17.
Later, when you ask has("[email protected]"), it all repeats: same formula → same number → same slot 17. Look inside — it's there. Whether you have a hundred addresses or a million, it's the same number of steps.
The formula always gives the same answer for the same key. That's why its result works as a ticket.
What if two keys land in the same slot
It happens. There's a finite number of slots and any number of keys. This is called a collision.
The fix is simple: each slot holds a tiny list. You arrive at slot 17, there are two addresses — compare both and find yours. As long as the table isn't overloaded, each slot holds one or two entries, and lookup stays instant.
When there are too many entries, the table grows on its own: it makes more slots and redistributes everything. You never notice — the language handles it.
Where you already use one
You write hash tables every day, just under other names:
MapandSetin JavaScript. The language spec explicitly requires lookups in them to be faster than scanning, and engines build them as hash tables.dictandsetin Python. Classic hash tables.- A plain JavaScript object. Also stores key-value pairs and looks up by key quickly.
- Indexes in a database. One kind of index is a hash index: "find the user with this email" without scanning the whole table.
- Caches. Already computed the answer? Find it by key and don't compute it again.
The trap: an object key always becomes a string
A hash function needs a key it can turn into a number. A plain JavaScript object solves this bluntly: it turns any key into a string.
const seats = {}
seats[1] = 'Anna'
seats['1'] = 'Boris'
console.log(seats[1]) // 'Boris'
The number 1 and the string '1' became one key. With objects as keys it gets worse:
const visits = {}
const userA = { id: 1 }
const userB = { id: 2 }
visits[userA] = 5
visits[userB] = 9
console.log(Object.keys(visits)) // ['[object Object]']
We checked: both users collapsed into the single key '[object Object]'. The first user's visit count was silently overwritten.
Map doesn't do this. It keeps the key as is — a number separate from a string, every object separate:
const visits = new Map()
visits.set(userA, 5)
visits.set(userB, 9)
visits.size // 2
What to take into your code
An everyday rule: if you keep asking "is X in this list" — put the list into a Set.
const known = new Set(emails) // once
known.has(email) // instant, as many times as you like
If the AI wrote includes or find inside a loop over a big list, that's the first thing to speed up. Ask: "Replace the array lookup inside the loop with a Set or Map."
Then why not store everything in hash tables?
They have no meaningful order and no position numbers. Need "the third one" or sorting — use an array. Need "is it there" and "find by key" — use a hash table.
Is a hash table the same as password hashing?
The idea is similar: a formula turns data into a number. But passwords need slow, cryptographically strong functions, while tables need the fastest ones possible. Different tools for different jobs.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





