Are there any improvements that can be made on the following to check the equality of two arrays in javascript?
"use strict"
function main(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false
}
}
return true;
}
const tests = [
[[1,2],[1,2]],
[[1],[1,2]],
[[1,2],[1]],
[[1,2],[3,4]],
[[],[]]
]
for (const [arr1, arr2] of tests) {
let res = main(arr1, arr2);
console.log(arr1, arr2, res);
}
2
Answers
Not sure this is an improvement, but you could make it shorter:
There is no further improvement in terms of the algorithm used to check array equality, AFAIK.