From 8ddd0616a99c1a8c4631cfe0f71ae64d38bfc76a Mon Sep 17 00:00:00 2001 From: rei2hu Date: Fri, 4 Oct 2019 09:39:56 -0500 Subject: [PATCH] fix(Util): make arraysEqual avoid mutating the input arrays (#3506) --- src/util/Util.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/util/Util.js b/src/util/Util.js index ada12fdac..481d0ffa4 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -84,9 +84,9 @@ class Util { } /** - * Checks whether the arrays are equal, also removes duplicated entries from b. - * @param {Array<*>} a Array which will not be modified. - * @param {Array<*>} b Array to remove duplicated entries from. + * Checks whether two arrays are equal or have the same elements. + * @param {Array<*>} a The first array. + * @param {Array<*>} b The second array. * @returns {boolean} Whether the arrays are equal. * @private */ @@ -94,12 +94,10 @@ class Util { if (a === b) return true; if (a.length !== b.length) return false; - for (const item of a) { - const ind = b.indexOf(item); - if (ind !== -1) b.splice(ind, 1); - } + const setA = new Set(a); + const setB = new Set(b); - return b.length === 0; + return a.every(e => setB.has(e)) && b.every(e => setA.has(e)); } /**