Comment

Comment by vanderZwan

It just so happens that I've been working on trying to build the fastest possible trie implementation in JavaScript to speed up `lz-string` and potentially `js-search`. The whole process I went through is too much to write down here, but you can basically read it here:

https://github.com/pieroxy/lz-string/pull/98

https://github.com/bvaughn/js-search/issues/42

The short version is: replace those single-char strings with the result of `charCodeAt()`, and then use `charCodeAt()` during search too. The reason is that integer key lookup is much faster than string key lookup:

https://stackoverflow.com/questions/10639488/faster-to-acces...

It's important that all of your keys are integers though, so the trick is to actually store/look up `charCodeAt()+1`, and then store the indices at key 0.

If you feel like taking a risk, you can make a slightly more complicated data structure where you replace the object with an array and do a linear search:

https://jsfiddle.net/vanderZwan/ccfxv4sg/1/

For most data this is the fastest, because the array will only be a handful of characters (except at the root). However, in certain special cases (specifically, if you have a lot of unique characters at the root) it becomes prohibitively slow.

parent vanderZwan 9 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton