4 ms·
I used to hate tries, mostly bc I refused to take the time to understand them, but it is a pretty cool concept with a narrow but very practical use case. It doe
by game_the0ry 5y ago
I used to hate tries, mostly bc I refused to take the time to understand them, but it is a pretty cool concept with a narrow but very practical use case. It does what it does really well, nothing more.
However, the idea of implementing in JS gives me the impression that the author is advocating implementing autocomplete on the client (browser) [1]. I would encourage developers not to do so. Depending on the data set to be rendered, that could be a big perf hit or not feasible in certain scenarios, but if the data set is not dynamic or has a small memory footprint (ie, states in the US), then it should be fine to have that data sent to the client to be processed on.
My general rule of thumb - limit business logic to the server and rendering logic to the browser.
You can implement server-side autocomplete - send the partial search string as a request to the server, which responds with the autocomplete list of strings to be rendered in the UI, debouncing [2] to limit the number of requests to the server (instead of requesting on every user change to the partial search string). Of course, this would have its trade offs (API reqs introduce their own perf concerns), but you want to go this route if your search data set is dynamic and sufficiently large.
[1] Not sure if that was the author's intention bc I cannot access the author's blog on my work machine.
[2] https://lodash.com/docs/4.17.15#debounce https://lodash.com/docs/4.17.15#debounce
- locusfox 5y agoI think this specific post serves more as a gentle introduction to Trie and it's underlying concepts rather than advocating how to use it for production apps.
- kitd 5y agoAgreed. Tools like htmx [1] are good for this. Combined with the "list" attr on <input>s makes for very simple autocomplete implementations. [1] https://htmx.org/examples/active-search/ https://htmx.org/examples/active-search/