Recently solved the problem when it was necessary to determine whether a certain (string) value falls into the set of valid values. In JavaScript 1.6, there is an Array object's indexOf method for searching in an array, but this method is not supported in Internet Explorer. For IE, this method is implemented by looping through the array. But since the order of the elements was not important and every millisecond was counted, I compared the performance of looping through the array with other search options (without iteration). And the search options without busting the following:
Gluing an array into a string and searching in a string
if (myarray.join().search( " " ) != -1) { ... }
* This source code was highlighted with Source Code Highlighter .
When using this method, it is necessary to take into account the problem of the uniqueness of the occurrence of a search string in a string glued from an array. For this you can, for example, use gluing symbols that are not a priori found in the string:
* This source code was highlighted with Source Code Highlighter .
Using an object instead of an array, "array values" are hash keys
if ( " " in myarray) { ... }
* This source code was highlighted with Source Code Highlighter .
To evaluate the performance of each of the three search methods, I created a test page on which 100,000 lines of the “StringN” type are created, where N is the line number, and then the string “6000000” is searched for. It turned out that the in operator works almost instantly (runtime is always 0), even in IE6!