1

I need to compare the elements from two arrays as follows:

arr1[0] ? arr2[0]
arr1[1] ? arr2[1]
arr1[2] ? arr2[2]
etc.

I wrote some code but it seems to be slow when I try to compare 1000 objects like this on each array :

{
   "id":"event707",
   "name":"Custom707",
   "type":"disabled",
   "default_metric":false,
   "participation":"disabled",
   "serialization":"always_record"
}

This is how my function looks like (just an example for two arrays with hard coded data).

function compare() {
  var step = 0;
  var fruits1 = [{"apple":25},{"bannana":36},{"orange":6}];
  var fruits2 = [{"apple":25},{"bannana":36},{"orange":6}];
  for(var i=0;i<fruits1.length;i++) {
    for(var j=step;j<fruits2.length;j++) {
      console.log("FRUIT1");
      console.log(JSON.stringify(fruits1[i]));
      console.log("FRUIT2");
      console.log(JSON.stringify(fruits2[j]));
      console.log("----------------------");
      if(JSON.stringify(fruits1[i])!== JSON.stringify(fruits2[j])) {
        //do something
      }
      step = step + 1;
      break;
    }
  }
}
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
Valip
  • 4,440
  • 19
  • 79
  • 150
  • 1
    The way you show your comparison, you only need one indexer, and then you can use i to compare fruits1[i] with fruits2[i]. I don't really see a reason / necessity for the step variable. So, if you need to compare based on the index as well (eg: fruits1[1] has to be equal with fruits2[1] then I think you need to simplify your algorithm). This way, you could already say that 2 arrays are not the same because of a length difference between both arrays, and if they are, exit the loop as soon as you have a mismatch on the same positions of your array – Icepickle Jul 31 '16 at 08:21
  • You're right, there is no need for the step variable. I changed my code and now the response time is lower by 1 minute ! Thank you! – Valip Jul 31 '16 at 09:10

4 Answers4

1

Simple function without library:

var arr1 = [1,2,3];
var arr2 = [1,2,4];

//This function takes one item, the index of the item, and another array to compare the item with.
function compare(item, index, array2){
  return array2[index] == item;
}

// the forEach method gives the item as first parameter
// the index as second parameter
// and the array as third parameter. All are optional.
arr1.forEach(function(item, index){
  console.log(compare(item, index, arr2));
});

Combine this with the answer Abdennour TOUMI gave, and you have an object comparison method :)

For simple objects you could use JSON.stringify(obj1) === JSON.stringify(obj2).

More info on object comparison can be found in this answer

Community
  • 1
  • 1
Randy
  • 9,419
  • 5
  • 39
  • 56
1

With an invention of Object.prototype.compare() and Array.prototype.compare() this job becomes a very simple task. Array compare can handle both primitive and reference type items. Objects are compared shallow. Let's see how it works;

Object.prototype.compare = function(o){
  var ok = Object.keys(this);
  return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
};
Array.prototype.compare = function(a){
  return this.every((e,i) => typeof a[i] === "object" ? a[i].compare(e) : a[i] === e);
};

var fruits1 = [{"apple":25},{"bannana":36},{"orange":6}],
    fruits2 = [{"apple":25},{"bannana":36},{"orange":6}];
console.log(fruits1.compare(fruits2));
Redu
  • 25,060
  • 6
  • 56
  • 76
0

Use underscore array functions. I would go with intersection

http://underscorejs.org/#intersection

Roysh
  • 1,542
  • 3
  • 16
  • 26
0

You can use the following static method for Object class : Object.equals

Object.equals=function(a,b){if(a===b)return!0;if(!(a instanceof Object&&b instanceof Object))return!1;if(a.valueOf()===b.valueOf())return!0;if(a.constructor!==b.constructor)return!1;for(var c in a)if(a.hasOwnProperty(c)){if(!b.hasOwnProperty(c))return!1;if(a[c]!==b[c]){if("object"!=typeof a[c])return!1;if(!Object.equals(a[c],b[c]))return!1}}for(c in b)if(b.hasOwnProperty(c)&&!a.hasOwnProperty(c))return!1;return!0};

console.log(
    `"[1,2,3] == [1,2,3]" ?`,Object.equals([1,2,3],[1,2,3])
);

console.log(
    `"[{"apple":25},{"bannana":36},{"orange":6}] == [{"apple":25},{"bannana":36},{"orange":6}]" ?`,Object.equals([{"apple":25},{"bannana":36},{"orange":6}], [{"apple":25},{"bannana":36},{"orange":6}])
);
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254