使用Javascript / jQuery的更快替代$ .inArray当模式匹配字符串 [英] Javascript / jQuery faster alternative to $.inArray when pattern matching strings

查看:127
本文介绍了使用Javascript / jQuery的更快替代$ .inArray当模式匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在Javascript中大阵的话(〜100,000),我希望能够快速返回基于文本模式其中的一个子集。

I've got a large array of words in Javascript (~100,000), and I'd like to be able to quickly return a subset of them based on a text pattern.

例如,我想返回所有与模式,使输入开头的单词 HAP 应该给我 [快乐, 幸福,新锐,等等等等] ,作为一个结果。

For example, I'd like to return all the words that begin with a pattern so typing hap should give me ["happy", "happiness", "happening", etc, etc], as a result.

如果有可能,我想整个阵列上做到这一点的没有迭代。

If it's possible I'd like to do this without iterating over the entire array.

像这样的东西是行不通的速度不够快:

Something like this is not working fast enough:

// data contains an array of beginnings of words e.g. 'hap'
$.each(data, function(key, possibleWord) {
 found = $.inArray(possibleWord, words);
 // do something if found
}

这是我怎么能快速降低设定为可能的匹配,没有遍历整个单词设置的任何想法?这个词数组是按字母顺序是否有帮助。

Any ideas on how I could quickly reduce the set to possible matches without iterating over the whole word set? The word array is in alphabetical order if that helps.

推荐答案

如果你只是想搜索prefixes恰好有该数据结构,如的特里和三元搜索树

If you just want to search for prefixes there are data structures just for that, such as the Trie and Ternary search trees

一个快速谷歌搜索以及一些有希望的Javascrit特里和自动完成实现露面

A quick Google search and some promissing Javascrit Trie and autocomplete implementations show up:

http://ejohn.org/blog/javascript-trie-performance-analysis /

自动完成一个线索

http://odhyan.com/blog/2010/11 /特里 - 实现在的JavaScript /

这篇关于使用Javascript / jQuery的更快替代$ .inArray当模式匹配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆