From 161fc3d0cd7d11f1c2c4c10181f10a00b01f763b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 09:28:33 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20O(n)=20calculation=20optimi?= =?UTF-8?q?zation=20in=20Dashboard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Combined `.map()`, `Math.max()`, and `.forEach()` into a single loop in `updateStatistics` to reduce array allocations and iteration overhead. Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com> --- frontend/dashboard.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/frontend/dashboard.js b/frontend/dashboard.js index 281b9a2..e20aa9a 100644 --- a/frontend/dashboard.js +++ b/frontend/dashboard.js @@ -130,19 +130,21 @@ function updateStatistics(subjects) { return; } - const scores = subjects.map(subject => subject.scoreValue); - const highest = Math.max(...scores); - - // 計算加權平均 + // 計算最高分與加權平均,合併迴圈以減少陣列分配與迭代,並避免 Math.max 展開大陣列時可能發生的 Stack Overflow + let highest = -Infinity; let totalWeightedScore = 0; let totalWeight = 0; - subjects.forEach(subject => { + for (let i = 0; i < subjects.length; i++) { + const subject = subjects[i]; 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;