This function takes a start index and a list of keys, and returns a function capable of determining if one of those keys is at the given index into some string. The compiled function takes the string to check and returns the length of the found key.
I have created a jsPerf at https://jsperf.com/object-vs-compiled-trie. My new code is about three times faster than what I was using before (creating slices and checking against an object).
This is a list of things I think could be done. Don't plan on me doing them any time soon (or ever).
- Speed up compilation. Currently it uses recursion because it was easier to write, but I think it could be written iteratively.
- Simplify the generated function. Currently it uses mostly
switch
es, and it puts abreak
on everycase
. However, some of the cases have the same code inside them, so it could be optimized to fall-through in some places. I think this could be done either before the code is generated by optimizing the trie (and then handling those optimizations when generating the code), or while the code is being created by comparing the generated strings. I don't know which would be easier, but the second is probably slower. If aswitch
was optimized down to only one case body (not including a default), it could even be turned into anif
statement. Sinceif
statements are used when there's only one option, it sometimes generates nestedif
statements. These could be combined fairly easily, though I don't think it would actually affect performance much.