-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Optimize statistics calculation by combining array iterations #157
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,24 @@ function updateStatistics(subjects) { | |
| return; | ||
| } | ||
|
|
||
| const scores = subjects.map(subject => subject.scoreValue); | ||
| const highest = Math.max(...scores); | ||
|
|
||
| // 計算加權平均 | ||
| // Combine max calculation and weighted average into a single pass | ||
| // to avoid multiple O(n) iterations and Math.max spread limitations. | ||
| 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; | ||
| } | ||
|
|
||
|
Comment on lines
+135
to
+146
|
||
| const weight = getSubjectWeight(subject.SubjectName); | ||
| totalWeightedScore += score * weight; | ||
| totalWeight += weight; | ||
| }); | ||
| } | ||
|
Comment on lines
+139
to
+150
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. For better readability and consistency with other parts of the codebase that use declarative iteration (e.g., Additionally, this change introduces a different behavior for the edge case where all scores are 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.
updateStatisticsbehavior changed but there are no unit tests covering statistics computation (avg/highest/total). Since this file already has tests for other helpers, consider adding a small DOM-based test (e.g., via JSDOM) to assertavgScore/highestScoreupdates for representative subject arrays, including large arrays and edge values.