

Dictionary Lookups in JavaScript - stanleydrew
http://ejohn.org/blog/dictionary-lookups-in-javascript/

======
eapen
Great explanation and practical sample of javascript features + HTML5's local
storage.

The only thing missing was converting the dictionary data into a trie. ;-)

~~~
pmjordan
_The only thing missing was converting the dictionary data into a trie. ;-)_

That's what I was thinking. I wonder what the memory and download footprint
for that would be in practice. Considering characters are represented by
string objects in JavaScript, it could get expensive, unless the runtime
interns short strings. If not, you could represent single letters with
integers, which are almost certainly interned or even use a non-pointer
representation. All those objects at each tree level might be a bit much
though.

~~~
jerf
In this situation, I'd seriously consider throwing down a binary blob as a
string that directly represents this data as whatever fancy structure you want
and writing the small handful of functions it would take to traverse that
"string" to do the lookup. Then you can do whatever optimal thing you want and
memory usage is very predictable.

Technically this is what his final solution converged on, it's just the least
fancy "fancy structure" there is.

It's a last resort, certainly, but in certain cases it's the simplest answer.
Be aware of encoding issues, though; UTF-8 technically can't carry arbitrary
binaries and you may have to work around some things a bit.

~~~
jacobolus
I'd recommend sending base64'd data down. Use this to encode/decode from
javascript strings of 1-byte-per-character data:

[https://github.com/mcarter/js.io/blob/master/packages/std/ba...](https://github.com/mcarter/js.io/blob/master/packages/std/base64.js)

~~~
gojomo
FYI, many browsers offer (native) 'atob()' and 'btoa()' functions for Base64
encode/decode that will outperform that JS code.

~~~
tjarratt
For what it's worth, the linked code on github appears to be specific to
node.js. Last I checked, there was no native function to convert strings to
and from base64, only buffers.

If you're writing this for client side, it would indeed be preferable to use
the native functions.

------
psadauskas
I would have probably stored all the words as keys in a Redis DB, and then
never would have discovered the neat ideas he did about having the dictionary
client-side.

~~~
bluesnowmonkey
That's funny because I got something very similar as a job interview question
the other day, and my first response was basically, "Put it in Redis. Next
question."

Yeah... I didn't get that job.

------
sync
Instead of shoving the whole dictionary onto the client side (as clever as
that is), he could also look up all the anagrams & subanagrams given a set of
letters and then match against that subset of words.

Check out [http://stackoverflow.com/questions/396005/word-anagram-
hashi...](http://stackoverflow.com/questions/396005/word-anagram-hashing-
algorithm/396077#396077) for an example

------
sharkton
Very interesting. The is a good solution for lots of things

------
uglycode
Jresig needs to take a data structures 101 course.

~~~
jonursenbach
Elaborate?

