Skip to content

⚡ Bolt: Replace O(N^2) array spread with amortized O(1) Map push in useSchedule#140

Open
anyulled wants to merge 1 commit intomainfrom
bolt/optimize-use-schedule-13518155474737365169
Open

⚡ Bolt: Replace O(N^2) array spread with amortized O(1) Map push in useSchedule#140
anyulled wants to merge 1 commit intomainfrom
bolt/optimize-use-schedule-13518155474737365169

Conversation

@anyulled
Copy link
Copy Markdown
Owner

@anyulled anyulled commented Apr 3, 2026

💡 What:
Replaced an array spread operation [...existing, session] inside a loop with an if/else block that pushes session into the existing array or creates a new one inside hooks/useSchedule.ts.

🎯 Why:
Using the array spread operator within a loop structure creates a new array on every iteration. This leads to an $O(N^2)$ time complexity for grouping and incurs a significant Garbage Collection (GC) overhead from constant intermediate array object creations. By mutating the existing array via push(), we achieve an amortized $O(1)$ append operation, speeding up the grouping logic.

📊 Impact:

  • Reduces the time complexity of the schedule grouping logic from $O(N^2)$ to $O(N)$.
  • Substantially reduces temporary object allocations, which lessens pressure on the garbage collector.

🔬 Measurement:

  • Run npm run test (including __tests__/hooks-useSchedule.test.ts) to ensure the grouping output format is unchanged.
  • Use a performance profiler on pages displaying the schedule (which relies on useSchedule.ts) over large mock datasets to observe improvements in synchronous Javascript execution time.

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

Summary by CodeRabbit

  • Refactor
    • Optimized internal schedule grouping logic for improved performance.

- Avoid creating new arrays on each loop iteration by pushing to existing arrays in `useSchedule`
- Resolves unnecessary allocations in schedule grouping logic

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
Copy link
Copy Markdown

vercel bot commented Apr 3, 2026

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

Project Deployment Actions Updated (UTC)
devbcn Ready Ready Preview, Comment Apr 3, 2026 8:39am

@qodo-code-review
Copy link
Copy Markdown

Review Summary by Qodo

Optimize schedule grouping with direct array mutation

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Replace O(N²) array spread with O(1) Map push operation
• Eliminate unnecessary array allocations in schedule grouping loop
• Reduce garbage collection pressure from intermediate objects
• Improve synchronous JavaScript execution performance
Diagram
flowchart LR
  A["Loop sessions"] --> B["Get existing array"]
  B --> C{"Array exists?"}
  C -->|No| D["Create new array"]
  C -->|Yes| E["Push to existing"]
  D --> F["Set in Map"]
  E --> F
  F --> G["O(1) amortized operation"]
Loading

Grey Divider

File Changes

1. hooks/useSchedule.ts Performance optimization +6/-2

Replace array spread with direct mutation in grouping

• Replaced array spread operator [...existing, session] with conditional logic
• Check if array exists in Map before creating or mutating
• Use push() method to append sessions to existing arrays
• Eliminates O(N²) complexity from repeated array allocations

hooks/useSchedule.ts


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Copy Markdown

qodo-code-review bot commented Apr 3, 2026

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 3, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: eb687d3c-f014-4f12-af45-ec4c9baaa8b9

📥 Commits

Reviewing files that changed from the base of the PR and between 81f3c53 and 725d4fd.

📒 Files selected for processing (1)
  • hooks/useSchedule.ts

📝 Walkthrough

Walkthrough

The getSchedule function optimization: instead of creating new arrays via spread operator when grouping sessions by time, the code now retrieves the existing array from the map and mutates it in-place using push(). The functional outcome remains unchanged.

Changes

Cohort / File(s) Summary
Schedule Grouping Optimization
hooks/useSchedule.ts
Refactored session grouping to use in-place array mutation (push()) instead of creating new arrays via spread operator on each insertion into the sessionsByTime map.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

size/XXL

Poem

🐰 Arrays grouped with speeds so fleet,
No spreading arrays in the heat,
Just push and mutate, quick and clean,
The fastest grouping you've seen! ⚡

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main optimization: replacing O(N²) array spread with O(1) Map push in useSchedule, which directly matches the changeset's core improvement.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt/optimize-use-schedule-13518155474737365169

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 and usage tips.

Copy link
Copy Markdown
Contributor

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

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 refactors the session grouping logic in hooks/useSchedule.ts to improve performance by mutating the existing session array instead of creating a new one with the spread operator. I have no feedback to provide.

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