Skip to content

⚡ Bolt: [performance improvement] Optimize updateStatistics array iterations - #124

Closed
alvin000009238 wants to merge 1 commit into
devfrom
bolt-optimize-update-statistics-13231556317909425004
Closed

⚡ Bolt: [performance improvement] Optimize updateStatistics array iterations#124
alvin000009238 wants to merge 1 commit into
devfrom
bolt-optimize-update-statistics-13231556317909425004

Conversation

@alvin000009238

Copy link
Copy Markdown
Owner

💡 What: Combined .map(), Math.max(), and .forEach() calls into a single for loop in updateStatistics inside frontend/dashboard.js.
🎯 Why: Iterating over the array three times and creating intermediate arrays causes unnecessary GC overhead and overhead in time complexity.
📊 Impact: Reduces time complexity constants and prevents one unnecessary array allocation.
🔬 Measurement: Code runs faster and creates fewer intermediate array objects.


PR created automatically by Jules for task 13231556317909425004 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 2, 2026 09:37

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

This PR optimizes the dashboard’s updateStatistics() hot path by reducing repeated iterations over the subjects array, aiming to lower GC pressure and improve runtime performance in the frontend.

Changes:

  • Replaced subjects.map(...) + Math.max(...) + forEach(...) with a single indexed for loop.
  • Computes both weighted average and highest score in one pass.
Comments suppressed due to low confidence (1)

frontend/dashboard.js:154

  • highest is initialized to -Infinity and only updated when scoreValue compares greater. If all subject.scoreValue values are NaN/undefined (possible via getNumericScore() returning Number(fallbackValue)), highest will remain -Infinity and be rendered to the UI. Consider tracking whether any finite score was seen (or checking Number.isFinite(highest) after the loop) and falling back to '--' (and optionally skipping non-finite scores in the weighted average too) to avoid displaying -Infinity/NaN to users.
    // 計算加權平均與最高分
    let highest = -Infinity;
    let totalWeightedScore = 0;
    let totalWeight = 0;

    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;

    document.getElementById('avgScore').textContent = weightedAvg.toFixed(1);
    document.getElementById('totalSubjects').textContent = subjects.length;
    document.getElementById('highestScore').textContent = highest;
}

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

@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 refactors the updateStatistics function in frontend/dashboard.js to calculate both the highest score and the weighted average in a single loop, replacing the previous multi-pass approach. The review feedback suggests using a more idiomatic for...of loop and utilizing Math.max within the loop to correctly handle NaN values and maintain behavioral parity with the original implementation.

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

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.

medium

While the manual for loop is efficient, using a for...of loop is more idiomatic in modern JavaScript and improves readability by removing the need for manual index management. Modern engines optimize for...of to perform nearly identically to a standard for loop for array iterations.

Additionally, consider using highest = Math.max(highest, score) to maintain behavioral parity with the original implementation regarding NaN values. The current if (score > highest) logic will ignore NaN scores, whereas the original Math.max(...scores) would result in NaN if any score was invalid. If all scores happen to be invalid, the current code would incorrectly display -Infinity in the UI.

    for (const subject of subjects) {
        const score = subject.scoreValue;

        highest = Math.max(highest, score);

        const weight = getSubjectWeight(subject.SubjectName);
        totalWeightedScore += score * weight;
        totalWeight += weight;
    }

@alvin000009238
alvin000009238 deleted the bolt-optimize-update-statistics-13231556317909425004 branch May 13, 2026 12:47
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