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 }); } }