Skip to content

⚡ perf: optimize mapping assignment results complexity#10

Merged
richielagito merged 2 commits into
mainfrom
perf/optimize-assignment-mapping-7104020538864131750
Apr 27, 2026
Merged

⚡ perf: optimize mapping assignment results complexity#10
richielagito merged 2 commits into
mainfrom
perf/optimize-assignment-mapping-7104020538864131750

Conversation

@richielagito
Copy link
Copy Markdown
Owner

💡 What: Replaced myResults.find inside assignments.map with a Map lookup keyed by assignment_id inside 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

Co-authored-by: richielagito <88770508+richielagito@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 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 27, 2026

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

Project Deployment Actions Updated (UTC)
upload-it-in Ready Ready Preview, Comment Apr 27, 2026 11:35am

Copy link
Copy Markdown

Copilot AI left a comment

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 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(...) inside assignments.map(...) with a Map keyed by assignment_id for 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.

Comment on lines +203 to +209
{(() => {
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;
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

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

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(...).

Copilot uses AI. Check for mistakes.
Comment thread scripts/benchmark.js
Comment on lines +1 to +6
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}` }));
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

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

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 uses AI. Check for mistakes.
@richielagito
Copy link
Copy Markdown
Owner Author

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown

Copilot AI commented Apr 27, 2026

@copilot apply changes based on the comments in this thread

Done in commit a9578fd:

  1. useMemo for resultsMap — Moved the Map construction out of the JSX IIFE into the component body as const resultsMap = useMemo(() => new Map(myResults.map(r => [r.assignment_id, r])), [myResults]);. The render block now uses a plain assignments.map(...) referencing resultsMap.get(ass.id).

  2. benchmark.js moved to scripts/ — Relocated from the repo root to scripts/benchmark.js to keep the root clean and make it more discoverable. Run it with node scripts/benchmark.js.

@richielagito
Copy link
Copy Markdown
Owner Author

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown

Copilot AI commented Apr 27, 2026

@copilot apply changes based on the comments in this thread

These changes were already applied in commit a9578fduseMemo memoizes resultsMap in the component body, and benchmark.js was moved to scripts/benchmark.js. No further changes are needed.

@richielagito richielagito merged commit 3411e9b into main Apr 27, 2026
2 of 3 checks passed
@richielagito richielagito deleted the perf/optimize-assignment-mapping-7104020538864131750 branch April 27, 2026 11:42
Copilot stopped work on behalf of richielagito due to an error April 27, 2026 11:42
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.

3 participants