Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  72] [ 6]  / answers: 1 / hits: 17309  / 14 Years ago, wed, october 20, 2010, 12:00:00

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



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



So say I have an string array like so:

var descArr = {flipping burgers, pumping gas, delivering mail};



and the user wants to search for: p



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.



Any ideas anyone?


More From » arrays

 Answers
100

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 burgerspumping gasdelivering mail

  • Regex: ([^]*ping[^]*)



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



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




  • String 11 flipping burgers12 pumping gas13 delivering mail

  • Regex: (d+) ([^]*ping[^]*)


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



[#95248] Monday, October 18, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;