-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Combine multiple O(n) array iterations in updateStatistics #121
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,22 @@ function updateStatistics(subjects) { | |
| return; | ||
| } | ||
|
|
||
| const scores = subjects.map(subject => subject.scoreValue); | ||
| const highest = Math.max(...scores); | ||
|
|
||
| // 計算加權平均 | ||
| // ⚡ Bolt: Combine multiple O(n) array iterations into a single loop | ||
| // to avoid intermediate array allocation and reduce iteration overhead. | ||
| 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
+139
to
+148
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While using a standard for (const subject of subjects) {
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; | ||
|
|
||
|
|
||
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.
highestis initialized to-Infinityand only updated whenscore > highest. If anysubject.scoreValueisNaN(possible ifgetNumericScore()returnsNaN), comparisons will be false andhighestmay remain-Infinity, which then gets rendered to the UI. Also,totalWeightedScore += score * weightwill becomeNaNand propagate into the average. Consider skipping non-finite scores (e.g.,Number.isFinite(score)) and, if no valid scores are found, render'--'(or 0) instead of-Infinity/NaN.