Skip to content

⚡ Bolt: Optimize statistics computation in dashboard - #126

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

⚡ Bolt: Optimize statistics computation in dashboard#126
alvin000009238 wants to merge 1 commit into
devfrom
bolt-optimize-dashboard-stats-6058037573955197853

Conversation

@alvin000009238

Copy link
Copy Markdown
Owner

💡 What:
Combined .map(), Math.max(), and .forEach() in updateStatistics into a single for loop in frontend/dashboard.js.

🎯 Why:
To avoid intermediate array allocations and reduce O(n) loops for calculating statistics. Additionally, it addresses a potential call stack vulnerability where Math.max(...scores) might throw a RangeError: Maximum call stack size exceeded if the subjects array is unusually large.

📊 Impact:
Reduces iteration passes from three down to one O(n) pass when rendering dashboard statistics, slightly decreasing memory footprint by removing intermediate array creation.

🔬 Measurement:
Run pnpm test to verify logic correctly functions and produces identical outputs as prior code. Visual inspection in browser console can also verify statistics block correctly updates.


PR created automatically by Jules for task 6058037573955197853 started by @alvin000009238

Combined `.map()`, `Math.max()`, and `.forEach()` in `updateStatistics`
into a single `for` loop. This avoids an intermediate array allocation
and reduces processing time for potentially large subject lists.

Also, it avoids a potential `RangeError: Maximum call stack size exceeded`
that could happen when using `Math.max(...scores)` with large arrays.

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:38

@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 optimize performance by consolidating multiple array operations into a single for...of loop. This change reduces the number of iterations and avoids intermediate array allocations. A review comment suggests removing the redundant and branded explanatory comment added during the refactoring to maintain a cleaner codebase.

Comment thread frontend/dashboard.js
Comment on lines +138 to +140
// ⚡ Bolt: Combined .map(), Math.max(), and .forEach() into a single loop to reduce O(n) iterations
// and avoid intermediate array allocations, improving processing speed for large subject lists.
for (const subject of subjects) {

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

This comment is redundant and contains tool-specific branding ("Bolt"). It describes the refactoring process rather than the logic itself. Since the code is straightforward and the optimization is clear from the implementation, this comment should be removed to keep the codebase clean. Version control history is the appropriate place for documenting such changes.

    for (const subject of subjects) {

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 statistics calculation by replacing multiple array passes (.map() + Math.max() + .forEach()) with a single for...of loop in updateStatistics, reducing allocations and avoiding Math.max(...scores) spread-argument limits for very large subject lists.

Changes:

  • Compute weighted average and highest score in a single loop.
  • Remove intermediate scores array allocation and Math.max(...scores) spread usage.
Comments suppressed due to low confidence (1)

frontend/dashboard.js:156

  • highest is initialized to -Infinity and only updated when score > highest. If any subject.scoreValue is NaN (possible because getNumericScore() can return NaN when the fallback is non-numeric), the comparison is always false and highestScore can end up displaying -Infinity even when the list is non-empty. Consider guarding with Number.isFinite(score) (and/or coercing score to a number) and falling back to '--' when no finite scores are found, to avoid this regression vs. the previous Math.max(...scores) behavior.
    // 計算加權平均與最高分
    let highest = -Infinity;
    let totalWeightedScore = 0;
    let totalWeight = 0;

    // ⚡ Bolt: Combined .map(), Math.max(), and .forEach() into a single loop to reduce O(n) iterations
    // and avoid intermediate array allocations, improving processing speed for large subject lists.
    for (const subject of subjects) {
        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.

@alvin000009238
alvin000009238 deleted the bolt-optimize-dashboard-stats-6058037573955197853 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