From a5f131227befec2258db4de2980206268a672125 Mon Sep 17 00:00:00 2001 From: merceyz Date: Sat, 10 Aug 2019 16:59:42 +0200 Subject: [PATCH] perf(helper): avoid unnecessary compares --- src/utils/helpers.js | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/utils/helpers.js b/src/utils/helpers.js index 32667d5..523a1c3 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -41,34 +41,27 @@ export function isNestedLogicalAndExpression(node) { export function getMostFrequentNode(operators) { let maxNode = null; let maxCount = 0; + let operators_n = operators.length; - operators.forEach((row, row_index) => { - row.forEach((col, col_index) => { - if (col_index === row.length - 1) return; + for (let y = 0, y_n = operators_n - 1; y < y_n; y++) { + for (let x = 0, row = operators[y], x_n = row.length - 1; x < x_n; x++) { + let col = row[x]; let count = 0; - operators.forEach((row2, row2_index) => { - row2.forEach((col2, col2_index) => { - // Don't compare against the last item (class) or row, col - if ( - col2_index === row2.length - 1 || - (row_index === row2_index && col_index === col2_index) - ) { - return; - } - - if (compareNodes(col, col2)) { + for (let y2 = y + 1; y2 < operators_n; y2++) { + for (let x2 = 0, row2 = operators[y2]; x2 < row2.length - 1; x2++) { + if (compareNodes(col, row2[x2])) { count += 1; } - }); - }); + } + } if (count > maxCount) { maxNode = col; maxCount = count; } - }); - }); + } + } return maxNode; }