Skip to content

Commit 4404dc5

Browse files
⚡ Bolt: Optimize statistics computation and prevent stack overflow
Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com>
1 parent f93682b commit 4404dc5

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

frontend/dashboard.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,24 @@ 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 multiple array iterations into a single loop.
134+
// This reduces O(3n) iterations to O(n), avoids creating an intermediate array,
135+
// and prevents Maximum Call Stack Size Exceeded errors from Math.max(...scores)
136+
// for large datasets.
137137
let totalWeightedScore = 0;
138138
let totalWeight = 0;
139+
let highest = -Infinity;
139140

140-
subjects.forEach(subject => {
141+
for (let i = 0; i < subjects.length; i++) {
142+
const subject = subjects[i];
141143
const score = subject.scoreValue;
144+
145+
if (score > highest) highest = score;
146+
142147
const weight = getSubjectWeight(subject.SubjectName);
143148
totalWeightedScore += score * weight;
144149
totalWeight += weight;
145-
});
150+
}
146151

147152
const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0;
148153

0 commit comments

Comments
 (0)