⚡ Bolt: Combine multiple O(n) array iterations into a single loop in dashboard - #164
⚡ Bolt: Combine multiple O(n) array iterations into a single loop in dashboard#164alvin000009238 wants to merge 1 commit into
Conversation
Co-authored-by: alvin000009238 <107313913+alvin000009238@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. |
There was a problem hiding this comment.
Pull request overview
Optimizes updateStatistics in the frontend dashboard by consolidating multiple array passes into a single loop to reduce overhead and avoid large-array spread usage.
Changes:
- Replaced
subjects.map(...)+Math.max(...scores)+forEach(...)with a single indexedforloop. - Computes
highest,totalWeightedScore, andtotalWeightin one pass.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Combine map, Math.max, and forEach into a single loop for performance | ||
| let highest = -Infinity; | ||
| let totalWeightedScore = 0; | ||
| let totalWeight = 0; | ||
|
|
||
| subjects.forEach(subject => { | ||
| for (let i = 0; i < subjects.length; i++) { | ||
| const subject = subjects[i]; | ||
| const score = subject.scoreValue; | ||
|
|
||
| if (score > highest) { | ||
| highest = score; | ||
| } | ||
|
|
There was a problem hiding this comment.
highest is initialized to -Infinity and later rendered directly. If any subject.scoreValue is undefined/NaN (possible via getNumericScore fallback), the comparison will never update highest, and the UI can display -Infinity. Consider tracking the max only for finite numeric scores and falling back to '--' when no valid max exists (or initialize from the first valid score).
| // Combine map, Math.max, and forEach into a single loop for performance | ||
| let highest = -Infinity; | ||
| let totalWeightedScore = 0; | ||
| let totalWeight = 0; | ||
|
|
||
| subjects.forEach(subject => { | ||
| for (let i = 0; i < subjects.length; i++) { | ||
| const subject = subjects[i]; | ||
| const score = subject.scoreValue; | ||
|
|
||
| if (score > highest) { | ||
| highest = score; | ||
| } | ||
|
|
||
| const weight = getSubjectWeight(subject.SubjectName); | ||
| totalWeightedScore += score * weight; | ||
| totalWeight += weight; | ||
| }); | ||
| } | ||
|
|
||
| const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0; |
There was a problem hiding this comment.
This refactor changes the statistics computation path (max + weighted average) but updateStatistics isn’t covered by the existing dashboard.test.js tests (which currently only cover getNumericScore/shortenName). Please add a test that exercises the updated loop logic (ideally by extracting the pure computation into a testable helper), including edge cases like a single subject and non-finite scores.
| const highest = Math.max(...scores); | ||
|
|
||
| // 計算加權平均 | ||
| // Combine map, Math.max, and forEach into a single loop for performance |
There was a problem hiding this comment.
PR description says to validate via pnpm run test, but this repo’s test script is defined in package.json and is run via npm test/npm run test (no pnpm tooling). The PR description should be updated to reflect the actual command so reviewers don’t get blocked.
There was a problem hiding this comment.
Code Review
This pull request optimizes the updateStatistics function in frontend/dashboard.js by replacing multiple array iterations (map, Math.max, and forEach) with a single for loop to calculate the highest score and weighted average simultaneously. I have no feedback to provide.
💡 What: Combined
.map(),Math.max(...scores), and.forEach()into a singleforloop in theupdateStatisticsfunction infrontend/dashboard.js.🎯 Why: Prevents potential
RangeError: Maximum call stack size exceededfrom using the spread operator on arrays and reduces multiple O(n) array passes to a single pass, which lowers execution time and minimizes memory overhead.📊 Impact: Reduces array iterations from 3 passes to 1 pass and eliminates intermediate array allocation from
.map().🔬 Measurement: Run the test suite (
pnpm run test) to verify statistics are correctly calculated with the new O(n) loop.PR created automatically by Jules for task 16185430466854334215 started by @alvin000009238