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,19 +130,24 @@ 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 multiple array iterations into a single loop.
134+ // This reduces O(3n) iterations to O(n), avoids creating an intermediate array,
135+ // and prevents Maximum Call Stack Size Exceeded errors from Math.max(...scores)
136+ // for large datasets.
137137 let totalWeightedScore = 0 ;
138138 let totalWeight = 0 ;
139+ let highest = - Infinity ;
139140
140- subjects . forEach ( subject => {
141+ for ( let i = 0 ; i < subjects . length ; i ++ ) {
142+ const subject = subjects [ i ] ;
141143 const score = subject . scoreValue ;
144+
145+ if ( score > highest ) highest = score ;
146+
142147 const weight = getSubjectWeight ( subject . SubjectName ) ;
143148 totalWeightedScore += score * weight ;
144149 totalWeight += weight ;
145- } ) ;
150+ }
146151
147152 const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0 ;
148153
You can’t perform that action at this time.
0 commit comments