From d61b020b5556ef7adf818eb7c411d2148e40ae31 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 24 Jun 2026 01:16:43 +0000 Subject: [PATCH] Optimize diffTests by pre-computing basenames and using Sets for intersection Pre-computes basenames for matched elements and uses a single pass algorithm using Set tracking for multiple conditions matching, replacing redundant iterations of .filter and .some containing O(N*M) bottlenecks. --- cli/commands/diff.mjs | 32 +++++++++++++++++++++++--------- cli/validators/docs-diff.mjs | 30 ++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/cli/commands/diff.mjs b/cli/commands/diff.mjs index 856eea1..8b3d33b 100644 --- a/cli/commands/diff.mjs +++ b/cli/commands/diff.mjs @@ -348,8 +348,9 @@ function diffTests(dir, config = {}) { // Glob-aware matching (documented entries are often patterns or basenames). const codeArr = [...codeTests]; - // PERFORMANCE OPTIMIZATION: Pre-compile regular expressions to avoid O(N*M) - // instantiation bottlenecks inside the nested .filter and .some loops below. + // PERFORMANCE OPTIMIZATION: Pre-compile regular expressions and basenames to avoid O(N*M) + // instantiation and string processing bottlenecks. We use Sets to track matches in a + // single pass cross-comparison instead of multiple nested .filter and .some loops. const docMatchers = [...docTests].map(docEntry => { const entry = String(docEntry).trim(); const hasSlash = entry.includes('/'); @@ -363,17 +364,30 @@ function diffTests(dir, config = {}) { }; }); - const matches = (matcher, codeRel) => { - const subject = matcher.hasSlash ? codeRel : basename(codeRel); - return matcher.rx.test(subject); - }; + const codeItems = codeArr.map(c => ({ + original: c, + basename: basename(c) + })); + + const matchedDocs = new Set(); + const matchedCode = new Set(); + + for (const m of docMatchers) { + for (const c of codeItems) { + const subject = m.hasSlash ? c.original : c.basename; + if (m.rx.test(subject)) { + matchedDocs.add(m.original); + matchedCode.add(c.original); + } + } + } return { title: 'Test Files', icon: '🧪', - onlyInDocs: docMatchers.filter(m => !codeArr.some(c => matches(m, c))).map(m => m.original), - onlyInCode: codeArr.filter(c => !docMatchers.some(m => matches(m, c))), - matched: docMatchers.filter(m => codeArr.some(c => matches(m, c))).map(m => m.original), + onlyInDocs: docMatchers.filter(m => !matchedDocs.has(m.original)).map(m => m.original), + onlyInCode: codeItems.filter(c => !matchedCode.has(c.original)).map(c => c.original), + matched: docMatchers.filter(m => matchedDocs.has(m.original)).map(m => m.original), }; } diff --git a/cli/validators/docs-diff.mjs b/cli/validators/docs-diff.mjs index f9fef2b..a899c54 100644 --- a/cli/validators/docs-diff.mjs +++ b/cli/validators/docs-diff.mjs @@ -170,8 +170,9 @@ function diffTests(dir, config) { // Exact-string comparison produced the false "N documented but not found". const codeArr = [...codeTests]; - // PERFORMANCE OPTIMIZATION: Pre-compile regular expressions to avoid O(N*M) - // instantiation bottlenecks inside the nested .filter and .some loops below. + // PERFORMANCE OPTIMIZATION: Pre-compile regular expressions and basenames to avoid O(N*M) + // instantiation and string processing bottlenecks. We use Sets to track matches in a + // single pass cross-comparison instead of multiple nested .filter and .some loops. const docMatchers = [...docTests].map(docEntry => { const entry = String(docEntry).trim(); const hasSlash = entry.includes('/'); @@ -188,15 +189,28 @@ function diffTests(dir, config) { }; }); - const matches = (matcher, codeRel) => { - const subject = matcher.hasSlash ? codeRel : basename(codeRel); - return matcher.rx.test(subject); - }; + const codeItems = codeArr.map(c => ({ + original: c, + basename: basename(c) + })); + + const matchedDocs = new Set(); + const matchedCode = new Set(); + + for (const m of docMatchers) { + for (const c of codeItems) { + const subject = m.hasSlash ? c.original : c.basename; + if (m.rx.test(subject)) { + matchedDocs.add(m.original); + matchedCode.add(c.original); + } + } + } return { title: 'Test Files', - onlyInDocs: docMatchers.filter(m => !codeArr.some(c => matches(m, c))).map(m => m.original), - onlyInCode: codeArr.filter(c => !docMatchers.some(m => matches(m, c))), + onlyInDocs: docMatchers.filter(m => !matchedDocs.has(m.original)).map(m => m.original), + onlyInCode: codeItems.filter(c => !matchedCode.has(c.original)).map(c => c.original), }; }