Skip to content

⚡ Bolt: Combine multiple O(n) array iterations into a single loop in dashboard - #164

Closed
alvin000009238 wants to merge 1 commit into
devfrom
bolt-optimize-dashboard-stats-16185430466854334215
Closed

⚡ Bolt: Combine multiple O(n) array iterations into a single loop in dashboard#164
alvin000009238 wants to merge 1 commit into
devfrom
bolt-optimize-dashboard-stats-16185430466854334215

Conversation

@alvin000009238

Copy link
Copy Markdown
Owner

💡 What: Combined .map(), Math.max(...scores), and .forEach() into a single for loop in the updateStatistics function in frontend/dashboard.js.
🎯 Why: Prevents potential RangeError: Maximum call stack size exceeded from 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

Co-authored-by: alvin000009238 <107313913+alvin000009238@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.

Copilot AI review requested due to automatic review settings April 12, 2026 09:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 indexed for loop.
  • Computes highest, totalWeightedScore, and totalWeight in one pass.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread frontend/dashboard.js
Comment on lines +133 to +145
// 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;
}

Copilot AI Apr 12, 2026

Copy link

Choose a reason for hiding this comment

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

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

Copilot uses AI. Check for mistakes.
Comment thread frontend/dashboard.js
Comment on lines +133 to 151
// 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;

Copilot AI Apr 12, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment thread frontend/dashboard.js
const highest = Math.max(...scores);

// 計算加權平均
// Combine map, Math.max, and forEach into a single loop for performance

Copilot AI Apr 12, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.

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

Copy link
Copy Markdown
Contributor

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

@alvin000009238
alvin000009238 deleted the bolt-optimize-dashboard-stats-16185430466854334215 branch May 13, 2026 12:37
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.

2 participants