In the spirit of the original question:
I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not.
I have been running performance tests on some of the more simple suggestions proposed here with the following results (fast to slow):
while (67%) by Tim Down
var i = a1.length;
while (i--) {
if (a1[i] !== a2[i]) return false;
}
return true
every (69%) by user2782196
a1.every((v,i)=> v === a2[i]);
reduce (74%) by DEIs
a1.reduce((a, b) => a && a2.includes(b), true);
join & toString (78%) by Gaizka Allende & vivek
a1.join('') === a2.join('');
a1.toString() === a2.toString();
half toString (90%) by Victor Palomo
a1 == a2.toString();
stringify (100%) by radtek
JSON.stringify(a1) === JSON.stringify(a2);
Note the examples below assumes the arrays are sorted, single-dimensional arrays.
.length
comparison has been removed for a common benchmark (adda1.length === a2.length
to any of the suggestions and you will get a ~10% performance boost). Choose whatever solutions that works best for you knowing the speed and limitation of each.Unrelated note: it is interesting to see people getting all trigger-happy John Waynes on the down vote button on perfectly legitimate answers to this question.