Skip to content

⚡ Bolt: [Performance] Optimize Map Editor Diffs (O(N²) to O(N))#157

Open
xb1g wants to merge 1 commit into
mainfrom
bolt/optimize-edit-map-diffing-1696303809515594833
Open

⚡ Bolt: [Performance] Optimize Map Editor Diffs (O(N²) to O(N))#157
xb1g wants to merge 1 commit into
mainfrom
bolt/optimize-edit-map-diffing-1696303809515594833

Conversation

@xb1g

@xb1g xb1g commented Mar 29, 2026

Copy link
Copy Markdown
Collaborator

💡 What:
Optimized the diffing algorithms used to detect changes in Maps during saving in app/map/[id]/edit/edit-page-client.tsx. The previous implementation iterated through elements comparing against other arrays utilizing .some() and .find(). The new implementation utilizes pre-calculated Maps and Sets before beginning the iteration loops. For Paths, it implements a composite string key utilizing ${source_node_id}-${destination_node_id} to represent unique edges.

🎯 Why:
Using .some() or .find() inside a .filter() or .forEach() loop inherently produces nested iteration resulting in O(N²) time complexity. On maps with large amounts of nodes, assessments, and connections, this causes severe UI lag and locks up the main browser thread during the calculation phase. Using Sets and Maps ensures O(1) key/value lookups resulting in O(N) total complexity.

📊 Impact:
Reduces the time complexity of diff detection from O(N*M) to O(N) where N and M are the sizes of the current and initial elements. Resolves frontend thread-blocking when generating the batch update payload for large learning maps.

🔬 Measurement:
Run the existing test suite and lint. The logical output remains completely identical. Performance measurements can be verified locally using React Profiler or Chrome DevTools while saving an extremely large learning map (e.g. >100 nodes/connections) before and after the change; execution time in the saveMap diffing phase should be consistently shorter.


PR created automatically by Jules for task 1696303809515594833 started by @xb1g

- Replaced `.some()` and `.find()` array operations inside iterative loops (`.filter`, `.forEach`) with pre-computed `Set` and `Map` lookups.
- Applied optimization to diffing nodes, paths (using composite `source-destination` string keys), content, assessments, and quiz questions.
- Time complexity of these updates reduced from O(N²) to O(N), resolving significant CPU overhead when processing large map payloads in `app/map/[id]/edit/edit-page-client.tsx`.

Co-authored-by: xb1g <70068561+xb1g@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 Mar 29, 2026

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated (UTC)
pseed Ready Ready Preview, Comment Mar 29, 2026 4:03am

@xb1g
xb1g force-pushed the bolt/optimize-edit-map-diffing-1696303809515594833 branch from d62fd79 to 77523a4 Compare May 11, 2026 17:28
Copilot AI review requested due to automatic review settings May 11, 2026 17:28
@vercel

vercel Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

Unable to deploy a commit from a private repository on your GitHub organization to the wachaa1319's projects team on Vercel, which is currently on the Hobby plan. In order to deploy, you can:

  • Make your repository public or
  • Upgrade to Pro. A Pro subscription is required to deploy from a private organization repository.

To read more about collaboration on Vercel, click here.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the Map Editor’s “generate batch update” diffing logic to reduce CPU time during saves on large learning maps by replacing nested array scans (.some() / .find() inside loops) with precomputed Set/Map lookups.

Changes:

  • Reworked node diffing to use Set/Map for delete/create/update detection.
  • Reworked path diffing to use Set lookups based on a composite (source_node_id, destination_node_id) key.
  • Reworked content, assessment, and quiz-question diffing to use Set/Map lookups per node/assessment.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
app/map/[id]/edit/edit-page-client.tsx Replaces O(N²) diffing patterns with Set/Map lookups to reduce save-time UI thread blocking on large maps.
.jules/bolt.md Documents the performance pattern change and rationale for future reference.

Comment thread .jules/bolt.md
**Learning:** Found multiple instances where `.filter(x => !array.includes(x))` was being used, which is O(N^2).
**Action:** Replace `array.includes` with `Set.has` when filtering large arrays to improve performance.

## 2025-02-28 - [Replaced Array .find() and .some() inside loops with O(1) Maps and Sets]
@xb1g
xb1g force-pushed the bolt/optimize-edit-map-diffing-1696303809515594833 branch 2 times, most recently from 0b52ea4 to dfa7959 Compare May 11, 2026 17:39
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 11, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
web dfa7959 May 11 2026, 06:20 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants