API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
JavaScript Are Arrays Equal
Almost immediately after writing out LotusScript "Are Arrays Equal" function (see this document) we had a need to do the same thing in JavaScript. Just for fun, we decided to write the code in the way that would normally be thought of (check if every element in array 1 is an element in array 2 and vice versa). We just wanted to see how long it would take. Here is the code we tried out.

DO NOT, UNDER ANY CIRCUMSTANCES, USE THIS CODE IN A PRODUCTION ENVIRONMENT. SCROLL DOWN FURTHER TO SEE THE CODE THAT YOU SHOULD USE INSTEAD.

function areArraysEqualBad(array1, array2) {
   if ( (!array1[0]) || (!array2[0]) ) {
      return false;
   }
   if (array1.length != array2.length) {
      return false;
   }
   for (var i=0; i<array1.length; i++) {
      foundIt = false;
      for (var j=0; j<array2.length; j++) {
         if ( (array1[i] == array2[j]) && (typeof array1[i] == typeof array2[j]) ) {
            foundIt = true;
            j=array2.length+1;
         }
      }
      if (!foundIt) { return false; }
   }
   for (var i=0; i<array2.length; i++) {
      foundIt = false;
      for (var j=0; j<array1.length; j++) {
         if ( (array1[j] == array2[i]) && (typeof array1[j] == typeof array2[i]) ) {
            foundIt = true;
            j=array1.length+1;
         }
      }
      if (!foundIt) { return false; }
   }
   return true;
}

When run against arrays of about 20 elements, the function above isn't too slow. But running it against an array of 1000 elements took around 5 seconds to verify equality. (Verifying inequality would depend on how quickly the inequality was found). And when we expanded the array to 3000 elements, it wouldn't even run. We got a nice warning message from Internet Explorer and quit the script.

On the other hand, the right way of doing this (shown below) took just over a second to verify equality on an array of 5000 elements and less than a second for anything with 3000 elements or less. The code follows the same logic as the LotusScript code, so you can refer here for an explanation.

This code below is what you should use in your production environment:

function areArraysEqual(array1, array2) {
   var temp = new Array();
   if ( (!array1[0]) || (!array2[0]) ) { // If either is not an array
      return false;
   }
   if (array1.length != array2.length) {
      return false;
   }
   // Put all the elements from array1 into a "tagged" array
   for (var i=0; i<array1.length; i++) {
      key = (typeof array1[i]) + "~" + array1[i];
   // Use "typeof" so a number 1 isn't equal to a string "1".
      if (temp[key]) { temp[key]++; } else { temp[key] = 1; }
   // temp[key] = # of occurrences of the value (so an element could appear multiple times)
   }
   // Go through array2 - if same tag missing in "tagged" array, not equal
   for (var i=0; i<array2.length; i++) {
      key = (typeof array2[i]) + "~" + array2[i];
      if (temp[key]) {
         if (temp[key] == 0) { return false; } else { temp[key]--; }
      // Subtract to keep track of # of appearances in array2
      } else { // Key didn't appear in array1, arrays are not equal.
         return false;
      }
   }
   // If we get to this point, then every generated key in array1 showed up the exact same
   // number of times in array2, so the arrays are equal.
   return true;
}