From 013d19b30fd482f3d72cfe58a9574e3cd9513497 Mon Sep 17 00:00:00 2001 From: Pedro Nastasi Date: Tue, 10 Feb 2026 14:18:08 +0000 Subject: [PATCH] improve duplicate detection in traverse function Signed-off-by: Pedro Nastasi --- .../operations/stitcher/buildEntitySearch.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.ts b/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.ts index bee0426a99..c8e09f3347 100644 --- a/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.ts +++ b/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.ts @@ -71,6 +71,11 @@ type Kv = { // "h.j": "l" export function traverse(root: unknown): Kv[] { const output: Kv[] = []; + // Use a Set for O(1) case-insensitive duplicate detection of synthetic + // boolean path keys (e.g. "metadata.tags.java"), instead of the previous + // O(n) Array.some() linear scan which caused O(n²) overall complexity + // and severe event loop blocking for entities with large arrays. + const seenPathKeys = new Set(); function visit(path: string, current: unknown) { if (SPECIAL_KEYS.includes(path)) { @@ -111,13 +116,9 @@ export function traverse(root: unknown): Kv[] { visit(path, item); if (typeof item === 'string') { const pathKey = `${path}.${item}`; - if ( - !output.some( - kv => - kv.key.toLocaleLowerCase('en-US') === - pathKey.toLocaleLowerCase('en-US'), - ) - ) { + const lowerKey = pathKey.toLocaleLowerCase('en-US'); + if (!seenPathKeys.has(lowerKey)) { + seenPathKeys.add(lowerKey); output.push({ key: pathKey, value: true }); } }