var length = 1e5;
var f32_a = Float32Array.from({length}, Math.random);
var f32_b = Float32Array.from({length}, Math.random);
var f64_a = Float64Array.from({length}, Math.random);
var f64_b = Float64Array.from({length}, Math.random);
var arr_a = Array.from({length}, Math.random);
var arr_b = Array.from({length}, Math.random);
for (let i = 0; i < length; i++) {
f32_a[i] = f32_b[i];
}
for (let i = 0; i < length; i++) {
f64_a[i] = f64_b[i];
}
for (let i = 0; i < length; i++) {
arr_a[i] = arr_b[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
iterate and set f32 | |
iterate and set f64 | |
iterate and set array |
Test name | Executions per second |
---|---|
iterate and set f32 | 3295.7 Ops/sec |
iterate and set f64 | 3232.3 Ops/sec |
iterate and set array | 3532.7 Ops/sec |
The benchmark represented in the provided JSON tests the performance of different data types during iteration and assignment operations in JavaScript. Specifically, it compares the performance of Float32Array
, Float64Array
, and standard JavaScript arrays when performing the same iterative operation—assigning values from one array to another.
Float32Array
for (let i = 0; i < length; i++) { f32_a[i] = f32_b[i]; }
Float64Array
for (let i = 0; i < length; i++) { f64_a[i] = f64_b[i]; }
Standard Array
for (let i = 0; i < length; i++) { arr_a[i] = arr_b[i]; }
From the benchmark results, we observe the following execution rates:
This indicates that the standard JavaScript array iteration performed better in this specific benchmark scenario, which can be counterintuitive but could be attributed to optimizations in the JavaScript engine for array handling. In typical use cases, one might expect that Typed Arrays would fare better due to their reduced memory footprint and specialized handling. However, the overhead of access and assignment patterns in this benchmark may have favored standard arrays in performance.
Other alternatives for handling numeric arrays in JavaScript could include:
Each of these alternatives comes with its own set of advantages and trade-offs based on precision, performance, and ease of use.