From a2d634eba855c705ac805dc01c53ae36b19e1fe9 Mon Sep 17 00:00:00 2001 From: release-clay Date: Tue, 18 Feb 2025 08:28:16 -0500 Subject: [PATCH] perf(utils): Use Set to make arrayDiff O(n+m) vs O(n*m) This is used in a couple places where the values for n and m could be medium size (e.g. when checking for new partitions within a topic), and we've seen it appear in some of our CPU profiling. So making this linear instead of quadratic should help slightly when checking topics with higher partition counts. --- src/utils/arrayDiff.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/arrayDiff.js b/src/utils/arrayDiff.js index 2b88ef360..d8ec16a93 100644 --- a/src/utils/arrayDiff.js +++ b/src/utils/arrayDiff.js @@ -2,9 +2,10 @@ module.exports = (a, b) => { const result = [] const length = a.length let i = 0 + const bSet = new Set(b) while (i < length) { - if (b.indexOf(a[i]) === -1) { + if (!bSet.has(a[i])) { result.push(a[i]) } i += 1