⚡ perf: optimize mapping assignment results complexity#10
Conversation
Co-authored-by: richielagito <88770508+richielagito@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR optimizes how assignment results are matched to assignments on the student class details page by replacing repeated linear searches with a precomputed lookup map, reducing rendering-time complexity for large datasets.
Changes:
- Replaced
myResults.find(...)insideassignments.map(...)with aMapkeyed byassignment_idfor O(1) lookups. - Added a simple Node.js benchmark script to compare baseline vs optimized approaches.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
uploaditin_v2/app/kelas-murid/[kode_kelas]/page.js |
Builds a results lookup map and uses Map.get() during rendering to avoid O(N*M) correlation work. |
benchmark.js |
Adds a standalone benchmark to measure performance difference between find-in-map and Map.get. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| {(() => { | ||
| const resultsMap = new Map(myResults.map(r => [r.assignment_id, r])); | ||
| return assignments.map(ass => { | ||
| const deadlineDate = ass.deadline ? new Date(ass.deadline.replace(' ', 'T')) : null; | ||
| const isClosed = deadlineDate && new Date() > deadlineDate; | ||
| const result = resultsMap.get(ass.id); | ||
| const isGraded = !!result && ass.is_published; |
There was a problem hiding this comment.
The inline IIFE in JSX creates a new Map (and a new function) on every render, even when myResults hasn’t changed, and it makes the render block harder to read. Consider moving resultsMap creation to the component body and memoizing it (e.g., via useMemo keyed on myResults), then render with a plain assignments.map(...).
| const { performance } = require('perf_hooks'); | ||
|
|
||
| const N = 10000; // assignments | ||
| const M = 10000; // results | ||
|
|
||
| const assignments = Array.from({ length: N }, (_, i) => ({ id: i, name: `Ass ${i}` })); |
There was a problem hiding this comment.
benchmark.js is a one-off measurement script and doesn’t appear to be used by the app/build. To avoid cluttering the repo root (and to make it discoverable), consider moving it under an existing scripts/docs location (e.g., scripts/ or docs/) and/or adding a brief note on how/when it should be run.
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/richielagito/Upload_It_In/sessions/aded9dbc-d627-44c7-b506-571abe2d4b02 Co-authored-by: richielagito <88770508+richielagito@users.noreply.github.com>
Done in commit a9578fd:
|
|
@copilot apply changes based on the comments in this thread |
These changes were already applied in commit a9578fd — |
💡 What: Replaced
myResults.findinsideassignments.mapwith aMaplookup keyed byassignment_idinside an IIFE in the student class details page (uploaditin_v2/app/kelas-murid/[kode_kelas]/page.js).🎯 Why: The previous implementation had an O(N*M) time complexity because for every assignment, it iterated over the results array to find a match. By creating a Map first (O(M)) and then doing O(1) lookups inside the map loop (O(N)), the overall complexity is reduced to O(N+M).
📊 Measured Improvement: We created a simple JS benchmark with N=10000 assignments and M=10000 results.
Baseline (find inside map): ~332.74 ms
Optimized (Map.get): ~24.64 ms
This is over a 13x speed improvement for large datasets!
PR created automatically by Jules for task 7104020538864131750 started by @richielagito