var N = 1000000;
var x = [], y = [], z = [];
var xt = new Float32Array(1000000);
var yt = new Float32Array(1000000);
var zt = new Float32Array(1000000);
var vectors = [];
for(var i = 0; i < N; i++){
x[i] = Math.random();
y[i] = Math.random();
z[i] = Math.random();
xt[i] = x[i];
yt[i] = y[i];
zt[i] = z[i];
vectors[i] = { x: x[i], y: y[i], z: z[i] };
}
var vector;
for (var i = 0, li=x.length; i < li; ++i) {
x[i] = 2 * x[i];
y[i] = 2 * y[i];
z[i] = 2 * z[i];
}
for (var i = 0, li=vectors.length; i < li; ++i) {
vector = vectors[i];
vector.x = 2 * vector.x;
vector.y = 2 * vector.y;
vector.z = 2 * vector.z;
}
for (var i = 0, li=x.length; i < li; ++i) {
xt[i] = 2 * xt[i];
}
for (var i = 0, li=x.length; i < li; ++i) {
yt[i] = 2 * yt[i];
}
for (var i = 0, li=x.length; i < li; ++i) {
zt[i] = 2 * zt[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
soa | |
aos | |
soa mark II |
Test name | Executions per second |
---|---|
soa | 93.3 Ops/sec |
aos | 6.5 Ops/sec |
soa mark II | 74.7 Ops/sec |
Let's dive into the benchmark analysis.
Benchmark Definition
The benchmark is comparing two approaches to scaling out a loop: Single-Assignment Optimization (SOA) and Arrays of Structures (AoS). In SOA, each iteration modifies a single variable that is assigned a new value in the next iteration. In AoS, each iteration works on an entire structure (in this case, an object with three properties) and assigns it to a temporary variable.
Benchmark Preparation Code
The preparation code creates four arrays: x
, y
, z
(with 1 million elements each), and xt
, yt
, zt
(also with 1 million elements each). It then populates these arrays with random values and copies the values from x
, y
, and z
to xt
, yt
, and zt
. Finally, it creates an array of objects (vectors
) with three properties: x
, y
, and z
.
Individual Test Cases
There are three test cases:
x
(or y
, or z
) and modifies each element directly.vectors
and works on an entire object (vector
). It copies the modified value back to the original array (xt
, yt
, or zt
).xt
, yt
, and zt
arrays, while leaving the x
, y
, and z
arrays unchanged.Options Compared
Pros and Cons
Library Used
In this benchmark, there is no explicit library used. However, the use of Float32Array
suggests that the benchmark targets modern JavaScript environments with optimized array support.
Special JS Features/Syntax
This benchmark does not explicitly use any special JavaScript features or syntax.
Other Alternatives
If you wanted to create a similar benchmark, you could consider alternative approaches, such as:
vectors
array.Keep in mind that these alternatives would require significant changes to the benchmark preparation code.