Skip to content

Commit 8098c6b

Browse files
perf: optimize updateStatistics array iterations
Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com>
1 parent 1a2420d commit 8098c6b

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

frontend/dashboard.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,21 @@ function updateStatistics(subjects) {
130130
return;
131131
}
132132

133-
const scores = subjects.map(subject => subject.scoreValue);
134-
const highest = Math.max(...scores);
135-
136-
// 計算加權平均
133+
// 計算加權平均與最高分
134+
let highest = -Infinity;
137135
let totalWeightedScore = 0;
138136
let totalWeight = 0;
139137

140-
subjects.forEach(subject => {
138+
for (let i = 0; i < subjects.length; i++) {
139+
const subject = subjects[i];
141140
const score = subject.scoreValue;
141+
142+
if (score > highest) highest = score;
143+
142144
const weight = getSubjectWeight(subject.SubjectName);
143145
totalWeightedScore += score * weight;
144146
totalWeight += weight;
145-
});
147+
}
146148

147149
const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0;
148150

0 commit comments

Comments
 (0)