When it comes to keyword search, the first thing that comes to mind is nothing more than using some character functions such as indexOf and replace, or at most adding some regular expressions. Although it is very simple to implement, have you ever carefully considered the efficiency issues behind it? For example, in keyword filtering in forums, generally the number of keywords to be filtered and the length of text to be detected are not large, so there is not much worth paying attention to in this instant process. But if the number of keywords is no longer a handful, but thousands, and the text to be detected is also a long one, the results are no longer so optimistic. Everyone knows that for every additional keyword, a full-text search will be added, and the final time spent will be far beyond the acceptable range.
Since we are considering that extreme keyword search, the usual one-by-one traversal search is obviously not feasible. Nowadays, JavaScript is used. It would be a shame for this language if it does not use Hash tables. With unique support for watches, you might as well take out a small amount of space in exchange for a large amount of time.
Let’s look at an example first. For example, there are the following keywords: foo1, foo2, bar1, bar2. Since space is exchanged for time, they must be preprocessed before searching. As mentioned earlier, JS’s flexible and efficient tables are obviously the most advantageous to use the tree structure. Even if you don’t understand it, it doesn’t matter. The final implementation structure is just like the following code. It’s also very familiar to be familiar with JSON: