function compareArrays()

in src/server/utils/jsUtils.js [65:110]


function compareArrays(a1, a2, compareOrder) {
    // If either are undefined, return false - don't consider undefined to equal undefined.
    if (typeof a1 === 'undefined' || typeof a2 === 'undefined') {
        return false;
    }

    if (!Array.isArray(a1) || !Array.isArray(a2) || a1.length !== a2.length) {
        return false;
    }

    // Strict comparison to compare references.
    if (a1 === a2) {
        return true;
    }

    if (compareOrder) {
        return a1.every(function (v, i) {
            return v === a2[i];
        });
    }

    var itemCounts = {};
    var isSimilar = true;

    a1.forEach(function (value) {
        if (!itemCounts[value]) {
            itemCounts[value] = 0;
        }

        ++itemCounts[value];
    });

    // Using Array.prototype.some() to break early if needed.
    a2.some(function (value) {
        if (!itemCounts[value]) {
            // If this value does not exist or its count is at 0, then a2 has an item that a1 doesn't have.
            isSimilar = false;
            return true; // Return true to break early.
        }

        --itemCounts[value];
        return false; // Return false to keep looking.
    });

    return isSimilar;
}