Hi Jaretin,
I doubt that your method with the "associative array" really results in less string comparisons. I suppose the opposite is the case. There are more comparisons, but they are done by the private functions of the array object and not visible.
But before I start to explain why I think this method results in more string comparisons, this technique should not be used in principle as the article
JavaScript "Associative Arrays" Considered Harmful referenced from
Array docs on Mozilla Developer Network explains. There are other articles in WWW which warn also using arrays in this manner because JavaScript does not really support associative arrays, it just looks so.
My method uses really an array of strings which is in real an array of pointers to strings and the strings are also arrays of characters. Your method uses arrays of objects. The objects have as member variables an identification string and the string value. So what happens on code
Code: Select all
asSingleWords[asWords[nActWord]] == null
internally in the array. I don't know it for sure because not knowing the code of JavaScript core, but from a C/C++ programmers point of view I suppose an iterator loop is executed comparing the index string returned by asWords[nActWord] with the id string of every object in the array. So the current word is with string compares compared against all previous added words. This is like comparing current word against all words left in the line. Only this code line would most likely result in the same number of string compares.
But depending on the result, the next code to execute is either
Code: Select all
asSingleWords[asWords[nActWord]] = asWords[nActWord];
or
Code: Select all
asMultipleWords[asWords[nActWord]] = asWords[nActWord];
Both code lines will most likely result in one more iteration from first to last object in the array with comparing the id strings of the objects with the index string to find the right object to store the string value, or add a new object with a new id string and new string value.
An array working with strings as index can't be faster than an array working with an integer as index.
However, I found it interesting that there was no error on execution of your script because of function trim(). According to
String methods documentation this method of the String object was added with JavaScript 1.8.1 and IDM states in help that JavaScript 1.7 is implemented in UltraEdit. I wrote this little script:
Code: Select all
var sResult = "";
if (typeof(String.trim) != "function") sResult = " not";
UltraEdit.messageBox("String method trim is" + sResult + " supported.");
I executed it and got displayed the message: "String method trim is supported." That is very interesting. It looks like IDM has updated to a new version of the JavaScript core without writing it anywhere. The questions are now: In which version was the JavaScript core updated? And which JavaScript version is now working in UltraEdit?