-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: O(n) calculation optimization in Dashboard #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| }); | ||
| } | ||
|
Comment on lines
+133
to
+147
|
||
|
|
||
| const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a
for...ofloop would improve readability while maintaining the performance benefits of a single pass. Additionally, replacing theifcondition withhighest = Math.max(highest, score)is more idiomatic and ensures consistentNaNhandling. If anyscoreValueisNaN,highestwill correctly becomeNaN, matching the behavior ofweightedAvgand the original implementation (which usedMath.max(...scores)).