88

I want to check if the two arrays are identical (not content wise, but in exact order).

For example:

 array1 = [1,2,3,4,5]
 array2 = [1,2,3,4,5]
 array3 = [3,5,1,2,4]

Array 1 and 2 are identical but 3 is not.

Is there a good way to do this in JavaScript?

palswim
  • 11,856
  • 6
  • 53
  • 77
ssdesign
  • 2,743
  • 6
  • 37
  • 53
  • 1
    i think some of the answers are going for "lines of code" efficiency, like larry k, while others are going for speed of execution efficiency. sadly, you didn't state which you were looking for :) – Peter Recore Oct 26 '10 at 17:01
  • @koopajah Didn't vote to close as the difference is that the older question is set comparison and this is set comparison along with order of contents. So set a == b && a set is in the same order as b. – David Feb 14 '13 at 13:02
  • You might want to check [Compare two Arrays Javascript - Associative](http://stackoverflow.com/questions/1107237/compare-two-arrays-javascript-associative) – Saul Oct 26 '10 at 16:52

2 Answers2

152

So, what's wrong with checking each element iteratively?

function arraysEqual(arr1, arr2) {
    if(arr1.length !== arr2.length)
        return false;
    for(var i = arr1.length; i--;) {
        if(arr1[i] !== arr2[i])
            return false;
    }

    return true;
}
palswim
  • 11,856
  • 6
  • 53
  • 77
  • 3
    It's safest, fastest, more flexible, always accurate, and actually *more* "elegant" that the `array.join()` approach -- once the function is defined. It's also less memory intensive, if that becomes an issue. – Brock Adams Oct 26 '10 at 17:40
  • 1
    Nice approach. There is a little issue: The variable i should go from arr1.length - 1 to 0, not from arr1.length to 0. – mimarcel Sep 09 '14 at 21:13
  • 2
    @mimarcel: The `i--` statement evaluates once before the iteration starts. – palswim Sep 10 '14 at 05:32
  • @palswim you are right! i dind't realise the ``i--`` has a double trick. :) – mimarcel Sep 11 '14 at 16:11
  • There's actually a **bug** when this function deals with nested arrays: `arraysEqual( [[1],[2,3]], [[1],[2,3]] )` will return false – Richard Aug 02 '15 at 04:38
  • I think it will only work in shorted array. in case unsorted array it will not work. – Aniruddha Das Sep 30 '16 at 18:42
  • @Richard: This answer provides a shallow check algorithm. Your comment begs the question about the nature of equality. Two arrays with the same contents are not equal in Javascript, so are the contents of `[[1],[2,3]]` and `[[1],[2,3]]` equal? No, because `[1]` is not equal to `[1]` (assuming you have instantiated those arrays separately). – palswim Jan 05 '18 at 00:18
  • 1
    @AniruddhaDas: This will only work on "sorted" arrays because [the question asked for equality in order](https://stackoverflow.com/q/4025893/393280), not just isomorphism. – palswim Jan 05 '18 at 00:26
48

You could compare String representations so:

array1.toString() == array2.toString()
array1.toString() !== array3.toString()

but that would also make

array4 = ['1',2,3,4,5]

equal to array1 if that matters to you

Adam
  • 43,763
  • 16
  • 104
  • 144
  • 22
    This is plain wrong as it seem to suggest that [ 1, 2 ] equals [ '1,2' ] and also equals [ 1, '2' ]..... etc. etc. – davidhadas Dec 25 '15 at 12:46
  • It overlooked the type of each item. – Ron Jan 23 '16 at 00:20
  • 16
    Just ran some quick tests and a `for` loop is much, *MUCH* faster than the `toString` method. In my tests, the worst-case scenario for the `for` loop was being `20x` faster. The worst case was the `for` loop being `80x` faster. So basically don't ever use this method, it's both wrong and slow :). – Maverick Nov 30 '16 at 03:14
  • 1
    Thanks, could be useful to quickly test 2 arrays and remain readable even if it's not THE perfect way to do it ;) – TOPKAT Jul 24 '17 at 21:13
  • 4
    Or `array1.join() == array3.join();` but this does not check for type – Noon Time May 29 '18 at 04:31
  • Not the ideal solution, but works when you know the type is correct, or (in my case) you WANT the arrays to match even when the object types are different. – Jacob Morris Nov 07 '19 at 21:55
  • actually this was useful to me as i am guaranteed to compare the same types. Looks light – WtFudgE Feb 12 '20 at 10:06