Skip to content

Commit c8919bb

Browse files
⚡ Bolt: Combine multiple O(n) loops in updateStatistics and fix Math.max risk
Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com>
1 parent f93682b commit c8919bb

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

frontend/dashboard.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,29 @@ function updateStatistics(subjects) {
130130
return;
131131
}
132132

133-
const scores = subjects.map(subject => subject.scoreValue);
134-
const highest = Math.max(...scores);
135-
136-
// 計算加權平均
133+
// ⚡ Bolt: Combined O(n) loops (map, max, forEach) into a single iteration
134+
// to reduce overhead, avoid RangeError in Math.max(...scores) for large arrays,
135+
// and minimize GC churn.
136+
let highest = -Infinity;
137137
let totalWeightedScore = 0;
138138
let totalWeight = 0;
139139

140-
subjects.forEach(subject => {
140+
for (let i = 0; i < subjects.length; i++) {
141+
const subject = subjects[i];
141142
const score = subject.scoreValue;
142143
const weight = getSubjectWeight(subject.SubjectName);
144+
145+
if (score > highest) highest = score;
146+
143147
totalWeightedScore += score * weight;
144148
totalWeight += weight;
145-
});
149+
}
146150

147151
const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0;
148152

149153
document.getElementById('avgScore').textContent = weightedAvg.toFixed(1);
150154
document.getElementById('totalSubjects').textContent = subjects.length;
151-
document.getElementById('highestScore').textContent = highest;
155+
document.getElementById('highestScore').textContent = highest === -Infinity ? 0 : highest;
152156
}
153157

154158
// 生成成績卡片

0 commit comments

Comments
 (0)