开发者

How to query faster a long list of data in javascript?

Here is something I would like to do, I have a file, which something like this: "key"-"content", each "key" is not unique for the content, one key can have zero or more content that much.... The file is about 200Kb, I convert it in array, and put it all in the javascript. When user type, I loop the array once to find out the result, but it is slow... Any suggestions on how to doing this? Thank you.

(Only client side javascript implementation is allowed, not开发者_StackOverflow社区 allow to use server to analysis the result and send back.)


If I understood you right, these links to articles of John Resig may help you. His problem was the poor performance, when looking for valid words in a big text file while typing them.

Part 1: Dictionary Lookups in JavaScript
Part 2: JavaScript Trie Performance Analysis


I assume that the user is typing something that is supposed to match the "key"? Or the "content"?

Assuming it's the key, then sort the keys and use a binary search. Once you get a hit (assuming a partial match, like, say, the first letter), just keep scanning until your matches fail. That's your result set.

If you're querying the content, then it's the same premise, but you need to invert the index and make the content pieces your keys, and sort those.


Can you use an associative array, with unique keys that point to arrays of possible values?

{ 'key1' => ['value1','value2','value3'],
  'key2' => ['value1','value2'],
  'key3' => ['value1'],
}

It means more overhead to parse the list, but I bet searching the list will be much faster. It should also use less memory, since you aren't duplicating all of the duplicate keys in memory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜