Skip to content

feat: [performance improvement] avoid flatmap in tags pages#340

Open
anyulled wants to merge 2 commits into
mainfrom
perf-tags-flatmap-1870318922122028322
Open

feat: [performance improvement] avoid flatmap in tags pages#340
anyulled wants to merge 2 commits into
mainfrom
perf-tags-flatmap-1870318922122028322

Conversation

@anyulled

@anyulled anyulled commented Jul 20, 2026

Copy link
Copy Markdown
Owner

💡 What:
Replaced chained .flatMap().find() and .flatMap().filter() operations in generateMetadata and TagPage components with nested loops and early bailouts (.some).

🎯 Why:
The original code was calling sessionGroups.flatMap((group) => group.sessions) multiple times per page request. flatMap creates an entirely new array in memory, which is then immediately thrown away after .find() or .filter(). This creates unnecessary O(N) memory allocation and Garbage Collection pressure, and forces multiple full O(N) traversals of the data instead of allowing for O(1) early bailouts when a matching tag is found.

📊 Impact:

  • Eliminates O(N) array allocation overhead from flatMap.
  • Allows generateMetadata to break early (O(1) best case) instead of waiting for full array generation.
  • Reduces CPU cycles by combining displayTag finding and filteredTalks accumulation into a single nested loop traversal.

🔬 Measurement:
Run npm run build to verify static page generation succeeds without memory spikes. Ensure unit tests and UI rendering remain fully functional.


PR created automatically by Jules for task 1870318922122028322 started by @anyulled

Summary by CodeRabbit

  • Bug Fixes

    • Improved tag matching so tag pages consistently recognize capitalization and formatting variations.
    • Preserved correct display names for matched tags and fallback URL slugs.
    • Maintained appropriate “not found” handling when no talks match a tag.
  • Performance

    • Reduced unnecessary processing when filtering talks on tag pages, improving efficiency for grouped data.

Refactored tag matching and filtering in app/[year]/tags/[tag]/page.tsx and app/2026/tags/[tag]/page.tsx to avoid chained .flatMap().find() and .flatMap().filter() operations, reducing memory allocations and multiple array traversals.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel

vercel Bot commented Jul 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
devbcn-nextjs Error Error Jul 20, 2026 8:38am

Request Review

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@anyulled, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 52 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9a3ced04-e5b9-4086-b997-2b80eae6d687

📥 Commits

Reviewing files that changed from the base of the PR and between 0f59b23 and f6bad13.

📒 Files selected for processing (1)
  • .jules/bolt.md
📝 Walkthrough

Walkthrough

The tag routes now normalize tag matching and traverse grouped sessions directly. Metadata and page rendering derive the display tag from the first match, while rendering accumulates matching talks in a typed array. The bolt guide documents alternatives to chained flatMap().find() and flatMap().filter().

Changes

Tag Matching Traversal

Layer / File(s) Summary
Metadata tag resolution
app/2026/tags/[tag]/page.tsx, app/[year]/tags/[tag]/page.tsx
Metadata generation scans session groups for normalized tag matches and derives displayTag from the first matched tag variant.
Single-pass page filtering
app/2026/tags/[tag]/page.tsx, app/[year]/tags/[tag]/page.tsx, .jules/bolt.md
Page rendering accumulates matching talks during grouped traversal, preserves the first matched display variant, and documents alternatives to chained flattening operations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Poem

A rabbit hops through grouped arrays,
No flattened trails or wasted snares.
First tags shine and talks collect,
Typed little lists now intersect.
“Less allocation!” the bunny sings,
While metadata grows matching wings.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: improving tag pages by avoiding chained flatMap operations for performance.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf-tags-flatmap-1870318922122028322

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes session filtering and matching by replacing chained .flatMap() operations with nested .some() and .forEach() loops to avoid unnecessary array allocations, documenting this pattern in .jules/bolt.md. The reviewer suggests further improving this by using nested for...of loops (with labeled breaks where applicable) instead of .some() and .forEach(). This would avoid closure allocation overhead, simplify the code by removing intermediate state objects, and make the implementation more idiomatic.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread .jules/bolt.md
## 2024-07-20 - Avoid chained flatMap().find() and flatMap().filter() on grouped data structures

**Learning:** When dealing with nested or grouped data structures (like `SessionGroup[]` where each group contains `Session[]`), chaining `.flatMap()` to flatten the arrays followed by `.find()` or `.filter()` causes the engine to allocate an entirely new flattened O(N) array in memory before performing the search. This introduces significant memory allocation overhead and multiple linear traversals.
**Action:** Instead of `flatMap().find()`, use nested loops or `groupedData.some(group => group.items.some(condition))` to allow for O(1) early bailout matching without pre-allocating a flattened array. For `.filter()`, use nested `.forEach()` loops and manually `.push()` matching items into a state array to perform the filtering in a single memory-efficient pass.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update the recommended action to suggest nested for...of loops instead of .some() or .forEach() with state objects/arrays, as for...of loops are cleaner, more idiomatic, and avoid closure allocation overhead.

Comment on lines +45 to +55
const state = { matchedTag: undefined as string | undefined };
sessionGroups.some((group) =>
group.sessions.some((talk) => {
const found = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
if (found) {
state.matchedTag = found;
return true;
}
return false;
})
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using nested for...of loops with a labeled break is cleaner and more idiomatic than using nested .some() calls with an external state object. It also avoids allocating closure functions on every iteration.

  let matchedTag: string | undefined;
  outer: for (const group of sessionGroups) {
    for (const talk of group.sessions) {
      const found = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
      if (found) {
        matchedTag = found;
        break outer;
      }
    }
  }

Comment on lines +75 to +93
const state = {
filteredTalks: [] as Talk[],
displayTag: undefined as string | undefined,
};

return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase());
sessionGroups.forEach((group) => {
group.sessions.forEach((talk) => {
const matchedTag = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
if (matchedTag) {
if (!state.displayTag) {
state.displayTag = matchedTag;
}
state.filteredTalks.push(talk);
}
});
});

const displayTag = state.displayTag ?? decodedTag.replaceAll("-", " ");
const filteredTalks = state.filteredTalks;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using nested for...of loops is cleaner and more performant than nested .forEach() calls. It avoids creating an intermediate state object and allocating closure functions for every group and session.

Suggested change
const state = {
filteredTalks: [] as Talk[],
displayTag: undefined as string | undefined,
};
return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase());
sessionGroups.forEach((group) => {
group.sessions.forEach((talk) => {
const matchedTag = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
if (matchedTag) {
if (!state.displayTag) {
state.displayTag = matchedTag;
}
state.filteredTalks.push(talk);
}
});
});
const displayTag = state.displayTag ?? decodedTag.replaceAll("-", " ");
const filteredTalks = state.filteredTalks;
const filteredTalks: Talk[] = [];
let matchedTag: string | undefined;
for (const group of sessionGroups) {
for (const talk of group.sessions) {
const found = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
if (found) {
if (!matchedTag) {
matchedTag = found;
}
filteredTalks.push(talk);
}
}
}
const displayTag = matchedTag ?? decodedTag.replaceAll("-", " ");

Comment on lines +52 to +62
const state = { matchedTag: undefined as string | undefined };
sessionGroups.some((group) =>
group.sessions.some((talk) => {
const found = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
if (found) {
state.matchedTag = found;
return true;
}
return false;
})
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using nested for...of loops with a labeled break is cleaner and more idiomatic than using nested .some() calls with an external state object. It also avoids allocating closure functions on every iteration.

  let matchedTag: string | undefined;
  outer: for (const group of sessionGroups) {
    for (const talk of group.sessions) {
      const found = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
      if (found) {
        matchedTag = found;
        break outer;
      }
    }
  }

Comment on lines +81 to +99
const state = {
filteredTalks: [] as Talk[],
displayTag: undefined as string | undefined,
};

return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase());
sessionGroups.forEach((group) => {
group.sessions.forEach((talk) => {
const matchedTag = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
if (matchedTag) {
if (!state.displayTag) {
state.displayTag = matchedTag;
}
state.filteredTalks.push(talk);
}
});
});

const displayTag = state.displayTag ?? decodedTag.replaceAll("-", " ");
const filteredTalks = state.filteredTalks;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using nested for...of loops is cleaner and more performant than nested .forEach() calls. It avoids creating an intermediate state object and allocating closure functions for every group and session.

Suggested change
const state = {
filteredTalks: [] as Talk[],
displayTag: undefined as string | undefined,
};
return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase());
sessionGroups.forEach((group) => {
group.sessions.forEach((talk) => {
const matchedTag = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
if (matchedTag) {
if (!state.displayTag) {
state.displayTag = matchedTag;
}
state.filteredTalks.push(talk);
}
});
});
const displayTag = state.displayTag ?? decodedTag.replaceAll("-", " ");
const filteredTalks = state.filteredTalks;
const filteredTalks: Talk[] = [];
let matchedTag: string | undefined;
for (const group of sessionGroups) {
for (const talk of group.sessions) {
const found = getTagsFromTalk(talk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagLower);
if (found) {
if (!matchedTag) {
matchedTag = found;
}
filteredTalks.push(talk);
}
}
}
const displayTag = matchedTag ?? decodedTag.replaceAll("-", " ");

Refactored tag matching and filtering in app/[year]/tags/[tag]/page.tsx and app/2026/tags/[tag]/page.tsx to avoid chained .flatMap().find() and .flatMap().filter() operations, reducing memory allocations and multiple array traversals.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant