Last active
June 14, 2023 20:32
-
-
Save dfkaye/ef6a3994ea3eee746785d16e28190193 to your computer and use it in GitHub Desktop.
an equals function for comparing two arrays in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 14 June 2023 | |
| // an equals function for comparing two arrays in JavaScript | |
| // how we compare two arguments for equality: | |
| // both are arrays | |
| // with same length | |
| // with same items at same indexes | |
| // why this does not exist in the JavaScript built-in space: sorting. | |
| // our algorithm assumes that if they differently sorted they are not equal, | |
| // because sometimes insertion order matters. | |
| Array.equals = function (a, b) { | |
| return Array.isArray(a) | |
| && Array.isArray(b) | |
| && a.length === b.length | |
| && a.every(function (v, i) { | |
| return v === b[i] | |
| }); | |
| }; | |
| // In real life we won't append `equals` as a function to the `Array` namespace. | |
| // A better approach in JS land would be an `equals` method in the `Array` *prototype* | |
| // so we could use `a.equals(b)`, as with this implementation... | |
| Array.prototype.equals = function(a) { | |
| if (!Array.isArray(a) || this.length !== a.length) { | |
| return false; | |
| } | |
| // micro-optimization: | |
| // instead of using the `every` method, use a faster fail loop | |
| // in case we have big arrays to compare... | |
| for (var i = 0, j = this.length - 1; i <= j; i++, j--) { | |
| if (this[i] !== a[i] || this[j] !== a[j]) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| /* test it out */ | |
| var a = ['1', 0]; | |
| var b = ['1', '3']; | |
| var c = Array.from(a); | |
| var s = a.slice().sort(); | |
| var t = c.slice().sort(); | |
| var o = {name: 'o'} | |
| var p = o; | |
| var q = Object.create(p); | |
| console.group("Array.equals"); | |
| console.assert(!Array.equals(1, 1), "primitives"); | |
| console.assert(!Array.equals([], [1]), "unequal lengths"); | |
| console.assert(!Array.equals(a, b), "should not equal"); | |
| console.assert(Array.equals(a, c), "should equal"); | |
| console.assert(!Array.equals(a, s), "differently sorted"); | |
| console.assert(Array.equals(s, t), "equal sort"); | |
| console.assert(!Array.equals([o], [q]), "different items"); | |
| console.assert(Array.equals([o], [p]), "same items"); | |
| console.groupEnd("Array.equals"); | |
| console.group("instance.equals"); | |
| console.assert(!a.equals(1), "primitives"); | |
| console.assert(!a.equals([]), "unequal lengths"); | |
| console.assert(!a.equals(b), "should not equal"); | |
| console.assert(a.equals(c), "should equal"); | |
| console.assert(!a.equals(s), "differently sorted"); | |
| console.assert(s.equals(t), "equal sort"); | |
| console.assert(![o].equals([q]), "different items"); | |
| console.assert([o].equals([p]), "same items"); | |
| console.groupEnd("instance.equals"); | |
| /* should see no errors */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment