Define the question before starting the timer

先定义问题,再启动计时器

This experiment compares two summation loops over a Float64Array: a conventional loop and a loop unrolled four ways. Both receive the same input and return the same result. Unrolling does not guarantee a speedup. The JIT compiler, hardware, thermal state, and background work all influence the outcome.

这个实验比较 Float64Array 上两种求和循环:普通循环与四路展开循环。输入、元素数量和返回值一致。循环展开不保证更快;现代 JIT 的优化选择、设备、热状态与后台任务都会影响结果。

The question is how these implementations behave in this browser at this input size, rather than which spelling is universally faster.

我们关心的是在当前浏览器、当前输入规模下的测量,而不是宣称一种写法在所有机器上都更好。

Measure on your own device

在你的设备上测一次

Not run yet. Results will be measured on this device.尚未运行。数据来自当前设备的实际测量。

Allocation and initialization are outside the timed region. Each implementation gets 12 warm-up calls, followed by 15 samples with alternating order. Each sample batches 16 summations. We report the median and interquartile interval per summation rather than selecting the fastest run. Integer inputs keep the sum within the exact representation range, and both implementations must return the same result.

数据分配与初始化不计入计时区域。先各预热 12 次,然后进行 15 轮测量,每轮交替运行顺序,每个样本包含 16 次求和。报告每次求和的中位数和四分位区间,而不是挑选最小值。整数输入的总和在精确表示范围内,两种实现的输出必须相等。

Why one timing is not enough

为什么一次计时不够?

performance.now() provides a monotonic timestamp in milliseconds, but browsers may reduce its precision. Batching short operations reduces relative timer overhead without eliminating environmental noise.

performance.now() 提供以毫秒表示的单调时间戳,适合测量耗时,但浏览器可能降低计时精度。把极短操作组成批次,可以降低计时器开销的相对影响,却不能消除运行环境的噪声。

Repeated samples reveal variation; warming up reduces the impact of initial compilation; alternating order reduces systematic bias from changes over time. None of this creates a controlled laboratory environment. The experiment runs in a Web Worker to reduce direct interference from page rendering.

多次采样观察分布;预热减轻首次编译的影响;交替顺序降低随时间变化带来的系统性偏差。这些做法仍不构成受控实验室环境。实验在 Web Worker 里运行,以减少页面绘制对测量的直接干扰。

Record the conditions

记录条件,才谈得上复现

The exported JSON includes time, browser information, logical processor count when available, array size, warm-up count, batch size, raw samples, checksums, and summary statistics. Browsers do not reliably expose the precise CPU model or clock rate. Record hardware, OS, power mode, and other workloads separately when sharing results.

导出的 JSON 包含时间、浏览器信息、逻辑处理器数量(若可用)、数组大小、预热轮数、批次大小、每个原始样本、校验和与汇总结果。浏览器无法可靠给出具体 CPU 型号与频率,因此请在分享时另外记录硬件、操作系统、电源模式和其他负载。

If differences are small or the intervals overlap substantially, repeat the experiment and assess stability. The reported ratio is not a statistical significance test.

若结果差异很小或区间明显重叠,应增加重复运行并观察稳定性。这里展示的比值不构成统计显著性检验。

Reference

参考资料

MDN · Performance.now() documents monotonic timestamps, units, and privacy-related precision limits.

MDN · Performance.now() 说明了时间戳的单调性、单位和隐私相关的精度限制。