通过优化大型JS字符串数组搜索? [英] optimize search through large js string array?

查看:414
本文介绍了通过优化大型JS字符串数组搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个拥有超过10,000个元素一个大的JavaScript字符串数组,
如何快速通过它搜索?

if I have a large javascript string array that has over 10,000 elements, how do I quickly search through it?

现在我有一个存储作业的描述一个JavaScript字符串数组,
我的M允许用户动态滤波器返回的列表,因为它们输入到输入框

Right now I have a javascript string array that stores the description of a job, and I"m allowing the user to dynamic filter the returned list as they type into an input box.

所以说,我有一个字符串数组,像这样:结果
VAR descArr = {烙牛肉饼,加油,发送邮件};

So say I have an string array like so:
var descArr = {"flipping burgers", "pumping gas", "delivering mail"};

和用户想要搜索:p的

我将如何能够搜索,在它迅速拥有10000+描述一个字符串数组?
显然,因为他们是我的描述无法说明数组进行排序,因此二进制搜索出来了。而且,由于用户可以通过PPI或字母的任意组合,这部分搜索手段搜索我不能用关联数组(即 searchDescArray [加油]
加快搜索

How would I be able to search a string array that has 10000+ descriptions in it quickly? Obviously I can't sort the description array since they're descriptions, so binary search is out. And since the user can search by "p" or "pi" or any combination of letters, this partial search means that I can't use associative arrays (i.e. searchDescArray["pumping gas"] ) to speed up the search.

任何想法吗?

推荐答案

由于在实际的浏览器经常EX pression发动机将坚果在速度方面,如何做呀?相反,数组的传递一个巨大的字符串,并用标识符分开的话。
例如:

As regular expression engines in actual browsers are going nuts in terms of speed, how about doing it that way? Instead of an array pass a gigantic string and separate the words with an identifer. Example:


  • 字符串烙牛肉饼,加油,发送邮件

  • 正则表达式:([^] *平[^] *)

  • String "flipping burgers""pumping gas""delivering mail"
  • Regex: "([^"]*ping[^"]*)"

通过交换机 / G 全球你得到所有比赛。确保用户不搜索你的字符串分隔符。

With the switch /g for global you get all the matches. Make sure the user does not search for your string separator.

您甚至可以添加一个id为字符串的东西,如:

You can even add an id into the string with something like:


  • 字符串11烙牛肉饼,12加油,13传送邮件

  • 正则表达式:(\\ D +)([^] *平[^] *)

例如: http://jsfiddle.net/RnabN/4/ (30000串,限制的结果100)

Example: http://jsfiddle.net/RnabN/4/ (30000 strings, limit results to 100)

这篇关于通过优化大型JS字符串数组搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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