Skip to content

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

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

⚡ Bolt: Combine multiple O(n) array iterations into a single loop#131
alvin000009238 wants to merge 1 commit into
devfrom
bolt-optimize-update-statistics-16223660044411969809

Conversation

@alvin000009238

Copy link
Copy Markdown
Owner

💡 What: Refactored updateStatistics to compute the highest score and weighted average in a single pass instead of using .map(), Math.max(), and .forEach().
🎯 Why: Iterating over the array three times and creating intermediate arrays is inefficient and triggers unnecessary garbage collection, especially on mobile devices with constrained memory or in high-frequency rendering scenarios.
📊 Impact: Reduces array iteration from O(3n) to O(n) and avoids allocating an intermediate scores array. This saves memory and prevents theoretical Maximum call stack size exceeded errors if the dataset were massive.
🔬 Measurement: Run node --test tests/frontend/dashboard.test.js to confirm the logic remains valid, and observe faster execution times in browser profilers during heavy initial dashboard renders.


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

…ons into a single loop

Replaced sequential array operations `.map()`, `Math.max()`, and `.forEach()` in `updateStatistics` with a single `for` loop to reduce array allocation overhead and minimize GC pressure.

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

@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 consolidating the calculation of the highest score and weighted average into a single for loop, which improves performance by reducing iterations. Additionally, the build asset hashes for CSS and JS files in public/index.html have been updated. A review comment suggests adding a check for NaN values within the loop to prevent them from corrupting the weighted average calculation.

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;
const weight = getSubjectWeight(subject.SubjectName);

if (score > highest) highest = score;

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

The current loop is susceptible to NaN poisoning. If any subject.scoreValue is NaN, the totalWeightedScore (and consequently the weightedAvg) will become NaN. While the highest calculation correctly ignores NaN values (since NaN > -Infinity is false), the average calculation does not. It's safer to skip NaN values to ensure the statistics remain valid even if some data is missing or malformed.

Suggested change
for (let i = 0; i < subjects.length; i++) {
const subject = subjects[i];
const score = subject.scoreValue;
const weight = getSubjectWeight(subject.SubjectName);
if (score > highest) highest = score;
totalWeightedScore += score * weight;
totalWeight += weight;
});
}
for (let i = 0; i < subjects.length; i++) {
const subject = subjects[i];
const score = subject.scoreValue;
if (Number.isNaN(score)) continue;
const weight = getSubjectWeight(subject.SubjectName);
if (score > highest) highest = score;
totalWeightedScore += score * weight;
totalWeight += weight;
}

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

Refactors the dashboard statistics calculation to compute highest score and weighted average in a single pass, reducing redundant array iterations and intermediate allocations.

Changes:

  • Refactored updateStatistics() to compute highest and weightedAvg in one loop.
  • Updated public/index.html to reference new hashed Vite build outputs (/dist/main-*.css and /dist/main-*.js).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
public/index.html Updates referenced hashed build assets for the frontend bundle.
frontend/dashboard.js Combines multiple O(n) passes into a single loop for statistics calculation.

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

Comment thread public/index.html
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit" async defer></script>
<script src="/theme-init.js"></script>
<link rel="stylesheet" href="/dist/main-bt8E1vXG.css" id="vite-css">
<link rel="stylesheet" href="/dist/main-D2sAJccc.css" id="vite-css">

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

vite.config.js has emptyOutDir: true, so a new build removes old hashed assets. This PR updates /dist/main-*.css and /dist/main-*.js in public/index.html, but other static pages under public/ (e.g. public/privacy.html) still reference older /dist/main-*.css hashes and will 404 / lose styling after deploy. Consider extending scripts/inject-hash.js to update all public/*.html asset references (or standardize the stylesheet tag so it can be rewritten everywhere).

Suggested change
<link rel="stylesheet" href="/dist/main-D2sAJccc.css" id="vite-css">
<link rel="stylesheet" href="/dist/main.css" id="vite-css">

Copilot uses AI. Check for mistakes.
Comment thread frontend/dashboard.js
Comment on lines +133 to +139
// 計算最高分與加權平均,將多次 O(n) 迴圈合併為單次,減少 GC 開銷並提升效能
let highest = -Infinity;
let totalWeightedScore = 0;
let totalWeight = 0;

subjects.forEach(subject => {
for (let i = 0; i < subjects.length; i++) {
const subject = subjects[i];

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

There are tests for frontend/dashboard.js, but the refactor changes observable behavior of updateStatistics (highest score + weighted average calculation). Please add a unit test (e.g., via JSDOM like tests/frontend/theme.test.js) that asserts avgScore, highestScore, and totalSubjects output for a small subject list to prevent regressions.

Copilot uses AI. Check for mistakes.
Comment thread frontend/dashboard.js
Comment on lines 151 to 154
document.getElementById('avgScore').textContent = weightedAvg.toFixed(1);
document.getElementById('totalSubjects').textContent = subjects.length;
document.getElementById('highestScore').textContent = highest;
document.getElementById('highestScore').textContent = highest === -Infinity ? '--' : highest;
}

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

Since the function returns early when subjects.length is 0, highest should never remain -Infinity for normal numeric inputs; the highest === -Infinity ? '--' : highest branch is effectively dead code and can hide unexpected NaN/invalid scores. Consider initializing highest from the first element (and iterating from index 1) so you can drop the sentinel/conditional and keep the display logic consistent.

Copilot uses AI. Check for mistakes.
@alvin000009238
alvin000009238 deleted the bolt-optimize-update-statistics-16223660044411969809 branch May 13, 2026 12:46
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