Listen up, Ninjas.
If your plan is to drag a billion usernames into the browser and filter() them like a street brawl in the town square, just sheath that idea. That is not stealth. That is yelling your location at the enemy camp while carrying a temple bell.
A billion-row search is won in the shadows. The dojo rule is simple:
The server guards the treasure. The frontend throws one shuriken, then
vanishes.
You do not slay the whole army. You take the head that matters and you are gone before the lanterns flicker.
First law of the clan: the haystack is not your backpack
This scroll is forbidden:
js
// Dishonor.
// Your tab will join its ancestors.
const matches = allUsernames.filter((name) =>
name.startsWith(query)
)
A billion handles is not a list. It is a mountain. Stuffing the mountain into RAM is how junior mercenaries become a loading spinner in the sky.
The Ninja does not carry the mountain. The Ninja asks the mountain a tiny question.
Know your quarry
Instagram-style handles are clean targets. Unique. Short. Prefix-shaped.
- habeeb
habeeb_lateef
habitat.ng
habibi.eats
The civilian types hab and expects a whisper of names, exact blade first. They do not want you to recite the census.
Honorable question
Names that start with hab. Eight only. If hab itself lives, put it on the throne.
Dishonorable question
Walk every soul in the empire and see if hab hides in their robes.
Prefix plus a limit is a surgical strike when the store has an index. “Contains” on a billion rows is a siege. Ninjas do not lay sieges. That is army work. Leave it.
The silent kata (what your UI actually does)
keystroke → hide in the reeds → kill the old request → ask for a prefix → paint eight shadows
Four techniques. Master them and you look instant. Miss them and you look like a tourist with cymbals.
Technique 1: Patience of the reed — debounce
Civilians tap h a b e e b like rain on a tin roof. If you leap at every drop, you are not a Ninja. You are a doorbell.
Wait until the rain pauses. Then strike once.
js
function debounce(fn, ms) {
let smokeBomb
return (...args) => {
clearTimeout(smokeBomb)
smokeBomb = setTimeout(() => fn(...args), ms)
}
}
const hunt = debounce((query) => {
loadSuggestions(query)
}, 200)
input.addEventListener("input", (e) => {
hunt(e.target.value.trim().toLowerCase())
})
200ms is a decent crouch. Too eager and you spam the dojo gates. Too sleepy and the user thinks you died behind the curtain.
Also: do not unsheathe the blade for one letter. h is not a mission. It is a rumor.
```js
if (query.length < 2) {
renderEmpty("Keep typing, Ninja. The shadows need more letters.")
return
}
Technique 2: Assassinate yesterday’s mission — abort
You sent a crow for hab. The civilian already typed habeeb. If the old crow lands last, your dropdown lies. Lying is for politicians. Ninjas deal in current intel.
js
let livingCrow = null
async function loadSuggestions(query) {
if (livingCrow) livingCrow.abort() // one puff of smoke. gone.
const controller = new AbortController()
livingCrow = controller
const url = /api/usernames?prefix=${encodeURIComponent(query)}&limit=8
try {
const res = await fetch(url, { signal: controller.signal })
const data = await res.json()
// The box may have changed while you were in the rafters
if (query !== input.value.trim().toLowerCase()) return
renderList(data.results)
} catch (err) {
if (err.name === "AbortError") return // honorable death
renderEmpty("The crow never returned.")
}
}
Clan proverb:
> The latest query wears the crown. Late mail is not wisdom. It is a ghost.
Technique 3: Pocket scrolls — cache
You already asked for hab ten seconds ago. Asking again is not diligence. It is amnesia.
js
const sleeve = new Map() // query → { inkedAt, results }
const FRESHNESS = 30_000
function peekScroll(query) {
const scroll = sleeve.get(query)
if (!scroll) return null
if (Date.now() - scroll.inkedAt > FRESHNESS) {
sleeve.delete(query)
return null
}
return scroll.results
}
async function loadSuggestions(query) {
const remembered = peekScroll(query)
if (remembered) {
renderList(remembered) // silent win
return
}
// fetch, then tuck the answer into the sleeve
sleeve.set(query, { inkedAt: Date.now(), results: data.results })
}
Cache the empty caves too. “Nobody named xyzzy99” is still intel. Do not kick the same hollow tree twice.
Keep the sleeve small. Fifty missions, then forget the oldest. You are hoarding whispers, not the imperial archive.
Technique 4: Eight shadows, not eight thousand
Your API should look like a note slipped under a door:
http
GET /api/usernames?prefix=hab&limit=8
{
"query": "hab",
"results": [
{ "username": "habeeb", "exact": true },
{ "username": "habeeb_lateef" },
{ "username": "habitat" },
{ "username": "habitat.ng" },
{ "username": "habibi.eats" }
]
}
Then you paint only what the night gave you:
function renderList(results) {
list.innerHTML = ""
if (results.length === 0) {
list.textContent = "No clan member starts with that name."
return
}
for (const row of results) {
const item = document.createElement("li")
item.textContent = "@" + row.username
if (row.exact) item.dataset.soke = "true" // the true heir
list.appendChild(item)
}
}
Eight <li>s is a tea ceremony. A thousand DOM nodes is a parade. Virtualize only if some sensei demands an admin scroll that can grow. Even then: pages from the server, never “summon everyone.”
What the mountain does while you crouch (know your allies)
You do not build this in the browser. You should still understand why the strike is cheap.
Sorted names are a line of soldiers in alphabetical armor:
gabe
habitat
habitat.ng
habeeb
habeeb_lateef
habibi.eats
hackclub
“Starts with hab” is not “interview every soldier.” It is:
1. Leap to the first name ≥ hab (the index is your grappling hook).
2. Walk forward while the name still bows to hab.
3. Stop at eight. Disappear.
A trie is a tree of letters. h → a → b and you are already in the right branch. The rest of the forest never hears your footsteps.
Exact habeeb is even quieter: one hash, one kill, go home.
So the frontend’s job is not “be a database.” It is “throw a prefix, not a novel.
Two missions, two blades
The dropdown is reconnaissance. Opening a profile is the raid.
js
// Smoke bomb: names only
GET /api/usernames?prefix=hab&limit=8
// Katana: one soul, after they choose
GET /api/users/habeeb
Do not strap a biography, a portrait gallery, and a follower census to every suggestion. Fat payloads are clanking armor. Ninjas wear black for a reason.
list.addEventListener("click", (e) => {
const name = e.target.textContent.replace(/^@/, "")
window.location.href = "/u/" + name
})
Rank the squad you have, not the empire
Once eight names kneel before you, you may sort that squad:
function sortSuggestions(query, rows) {
return [...rows].sort((a, b) => {
if (a.username === query) return -1
if (b.username === query) return 1
return a.username.length - b.username.length
})
}
Exact match on the throne. Short names next. Popularity if the API handed you a score.
Fuzzy “did you mean habeeb?” is dessert on those eight plates. Running Levenshtein across a billion rows in the tab is how you become a cautionary campfire story.
Empty air is still a message
js
function decideWhatToShow(query) {
if (query.length === 0) return "recent" // local ghosts
if (query.length < 2) return "hint" // “more letters, traveler”
return "network"
}
Recent hunts live in the village, not the mountain:
const recent = JSON.parse(localStorage.getItem("recentHandles") || "[]")
function remember(username) {
const next = [username, ...recent.filter((x) => x !== username)].slice(0, 5)
localStorage.setItem("recentHandles", JSON.stringify(next))
}
Five names in localStorage. That is a pocket. That is allowed.
The full night raid (assemble the forms)
js
const sleeve = new Map()
let livingCrow = null
const onType = debounce((raw) => {
const query = raw.trim().toLowerCase()
if (query.length < 2) {
if (livingCrow) livingCrow.abort()
renderHint("Two letters, Ninja. Then we hunt.")
return
}
const scroll = sleeve.get(query)
if (scroll && Date.now() - scroll.inkedAt < 30_000) {
renderList(scroll.results)
return
}
loadSuggestions(query)
}, 200)
async function loadSuggestions(query) {
if (livingCrow) livingCrow.abort()
const controller = new AbortController()
livingCrow = controller
renderHint("Moving through the trees…")
const url =
/api/usernames?prefix=${encodeURIComponent(query)}&limit=8
try {
const res = await fetch(url, { signal: controller.signal })
const data = await res.json()
if (query !== currentQuery()) return
sleeve.set(query, { inkedAt: Date.now(), results: data.results })
renderList(data.results)
} catch (err) {
if (err.name !== "AbortError") renderHint("The path is blocked.")
}
}
No mountain in the backpack. No honor lost.
Scroll of shame (do not)
| Tempting jutsu | Why the elders will sigh |
|---|---|
| Load all usernames locally | You become the mountain. The mountain wins. |
| Fetch on every key | You are a festival drum, not a Ninja. |
| Client-side %term% / regex over the world | Siege warfare. Resign. |
| Return 1,000 hits “just in case” | JSON avalanche. DOM parade. Shame. |
| Ignore abort | Ghost intel overwrites living intel. |
| Fuzzy-match the entire table in the tab | Your laptop achieves enlightenment, then death. |
Final proverb
A billion-row search is fast when the Ninja never meets a billion rows.
Crouch. Cancel. Ask for a prefix of eight names. Paint them. Disappear.
The index is the silent ally in the rafters. Your job is not to be louder than the database. Your job is to be narrower, later, and true.
Now go, Ninjas. Type hab. Take habeeb. Leave no footprints in the DOM.