Community JavaScript Snippet

The Trie I Built for Search-Bar Autocomplete

We had 80k product names and a 600ms p95 on prefix lookup. A trie keyed by lowercased characters with per-node frequency counts cut it to 4ms and let us rank suggestions by popularity in the same pass.

The Trie I Built for Search-Bar Autocomplete

We had 80k product names and a 600ms p95 on prefix lookup. A trie keyed by lowercased characters with per-node frequency counts cut it to 4ms and let us rank suggestions by popularity in the same pass.

JavaScript
Frontend
3 snippets
trie
autocomplete
search
code-template
ethanhadid

By @ethanhadid

April 25, 2026

·

Updated May 18, 2026

918 views

29

Rate

A trie is a tree where each path from the root spells a stored word. The children map is what makes prefix lookup O(prefix.length): we walk one node per character without scanning siblings. Storing the original word at the terminal (rather than reconstructing it from the path) saves us a string-build on every result; for 50 suggestions in the dropdown that is the difference between 4ms and 12ms. Normalizing to lowercase on both insert and lookup is what makes "apple" and "Apple" match without a separate case-folding index.