Commit 708f09cf6a9 for nodejs
commit 708f09cf6a92c564eb09c65ddf3cc1655cd6bb80
Author: Livia Medeiros <livia@cirno.name>
Date: Fri Nov 6 00:54:02 2026 +0800
lib: replace Kahan summation with `MathSumPrecise`
Signed-off-by: LiviaMedeiros <livia@cirno.name>
PR-URL: https://github.com/nodejs/node/pull/67222
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
diff --git a/lib/internal/bench_runner/benchmark.js b/lib/internal/bench_runner/benchmark.js
index 0ab3da9f24d..cea0abc71e5 100644
--- a/lib/internal/bench_runner/benchmark.js
+++ b/lib/internal/bench_runner/benchmark.js
@@ -13,12 +13,14 @@ const {
MathMin,
MathRound,
MathSqrt,
+ MathSumPrecise,
Number,
NumberIsFinite,
NumberMAX_SAFE_INTEGER,
ObjectFreeze,
ObjectKeys,
PromiseWithResolvers,
+ SafeArrayIterator,
SafeSet,
StringPrototypeToLowerCase,
} = primordials;
@@ -485,18 +487,6 @@ class BenchContext {
}
}
-function arithmeticMean(values) {
- let sum = 0;
- let compensation = 0;
- for (let i = 0; i < values.length; i++) {
- const adjusted = values[i] - compensation;
- const next = sum + adjusted;
- compensation = (next - sum) - adjusted;
- sum = next;
- }
- return sum / values.length;
-}
-
function summarizeSamples(samples) {
const rates = [];
let min = Infinity;
@@ -508,7 +498,7 @@ function summarizeSamples(samples) {
max = MathMax(max, rate);
}
- const mean = arithmeticMean(rates);
+ const mean = MathSumPrecise(new SafeArrayIterator(rates)) / rates.length;
let variance = 0;
for (let i = 0; i < rates.length; i++) {
const difference = rates[i] - mean;