Skip to content

⚡ Optimize code sorting performance#18

Closed
thib3113 wants to merge 1 commit intomainfrom
perf/optimize-code-sorting-1911020232021369281
Closed

⚡ Optimize code sorting performance#18
thib3113 wants to merge 1 commit intomainfrom
perf/optimize-code-sorting-1911020232021369281

Conversation

@thib3113
Copy link
Owner

@thib3113 thib3113 commented Feb 2, 2026

💡 What:
Optimized the sortCodesByPriority function used in the useCodeLogic hook. I refactored the sorting logic into a reusable sortCodes function in src/utils/codeUtils.ts and implemented the Schwartzian transform (Map-Sort-Map) pattern.

🎯 Why:
The previous implementation created two new Date() objects for every comparison in the sort loop. For a list of N items, this resulted in $O(N \log N)$ date parsings, which is computationally expensive. The optimization reduces date parsing to $O(N)$ by pre-calculating the timestamps and priority values before sorting.

📊 Measured Improvement:
I created a benchmark (src/tests/perf_sort.bench.test.ts) comparing the old and new approaches with 10,000 items.

  • Baseline: ~133ms
  • Optimized: ~15ms
  • Speedup: ~8.5x

The logic regarding priority groups and NaN handling for invalid dates was strictly preserved.


PR created automatically by Jules for task 1911020232021369281 started by @thib3113

Optimized `sortCodesByPriority` by using the Schwartzian transform (Map-Sort-Map) pattern.
This avoids creating `new Date()` objects repeatedly (O(N log N)) during the sort comparison, reducing it to O(N).
Benchmark shows ~8.5x speedup (133ms -> 15ms for 10k items).

- Extracted `sortCodes` to `src/utils/codeUtils.ts`
- Updated `src/hooks/useCodeLogic.ts` to use the utility
- Added `src/tests/perf_sort.bench.test.ts` for verification
@google-labs-jules
Copy link
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.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2026

🚀 Deploy Preview Ready!

App available at: https://thib3113.github.io/bwb/pr-18/

@thib3113 thib3113 closed this Feb 4, 2026
@thib3113 thib3113 deleted the perf/optimize-code-sorting-1911020232021369281 branch February 4, 2026 07:45
github-actions bot added a commit that referenced this pull request Feb 4, 2026
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.

1 participant