File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -130,25 +130,29 @@ function updateStatistics(subjects) {
130130 return ;
131131 }
132132
133- const scores = subjects . map ( subject => subject . scoreValue ) ;
134- const highest = Math . max ( ...scores ) ;
135-
136- // 計算加權平均
133+ // ⚡ Bolt: Combined O(n) loops (map, max, forEach) into a single iteration
134+ // to reduce overhead, avoid RangeError in Math.max(...scores) for large arrays,
135+ // and minimize GC churn.
136+ let highest = - Infinity ;
137137 let totalWeightedScore = 0 ;
138138 let totalWeight = 0 ;
139139
140- subjects . forEach ( subject => {
140+ for ( let i = 0 ; i < subjects . length ; i ++ ) {
141+ const subject = subjects [ i ] ;
141142 const score = subject . scoreValue ;
142143 const weight = getSubjectWeight ( subject . SubjectName ) ;
144+
145+ if ( score > highest ) highest = score ;
146+
143147 totalWeightedScore += score * weight ;
144148 totalWeight += weight ;
145- } ) ;
149+ }
146150
147151 const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0 ;
148152
149153 document . getElementById ( 'avgScore' ) . textContent = weightedAvg . toFixed ( 1 ) ;
150154 document . getElementById ( 'totalSubjects' ) . textContent = subjects . length ;
151- document . getElementById ( 'highestScore' ) . textContent = highest ;
155+ document . getElementById ( 'highestScore' ) . textContent = highest === - Infinity ? 0 : highest ;
152156}
153157
154158// 生成成績卡片
You can’t perform that action at this time.
0 commit comments