var N = 1000000
var x = new Float32Array(N);
var y = new Float32Array(N);
var z = new Float32Array(N);
var interlaced = new Float32Array(3*N);
var vectors = [];
for (var i = 0, li=x.length; i < li; ++i) {
x[i] = Math.random();
y[i] = Math.random();
z[i] = Math.random();
vectors.push( {x:Math.random(), y:Math.random(), z:Math.random()} );
}
for (var i = 0, li=interlaced.length; i < li; ++i) {
interlaced[i] = Math.random();
}
var vector;
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;
}
let x1 = x;
let y1 = y;
let z1 = z;
for (var i = 0, li=x1.length; i < li; ++i) {
x1[i] = 2 * x1[i];
y1[i] = 2 * y1[i];
z1[i] = 2 * z1[i];
}
let x2 = x;
let y2 = y;
let z2 = z;
for (var i = 0, li=x2.length; i < li; ++i) {
x2[i] = 2 * x2[i];
}
for (var i = 0, li=y2.length; i < li; ++i) {
y2[i] = 2 * y2[i];
}
for (var i = 0, li=z2.length; i < li; ++i) {
z2[i] = 2 * z2[i];
}
let interlaced2 = interlaced;
for (var i = 0, li=interlaced2.length; i < li; ++i) {
interlaced2[i] = 2*interlaced2[i];
}
let interlaced3 = interlaced;
for (var i = 0, li=interlaced3.length; i < li; i+=3) {
interlaced3[i] = 2*interlaced3[i];
interlaced3[i+1] = 2*interlaced3[i+1];
interlaced3[i+2] = 2*interlaced3[i+2];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
AoS | |
SoA | |
SoA - one component per loop | |
Interlaced Array - no loop unrolling | |
Interlaced Array - with loop unrolling |
Test name | Executions per second |
---|---|
AoS | 92.1 Ops/sec |
SoA | 145.2 Ops/sec |
SoA - one component per loop | 157.9 Ops/sec |
Interlaced Array - no loop unrolling | 158.7 Ops/sec |
Interlaced Array - with loop unrolling | 153.5 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Definition
The benchmark measures the performance of multiplying lists of vectors in three different ways:
x
, y
, and z
properties, and there's a single array containing all these objects.x
, y
, and z
properties, just like in SoA, but they're stored in separate arrays for each component (x
, y
, and z
).Options Compared
The benchmark compares the performance of these three approaches:
x
, y
, and z
).Pros and Cons
Here's a brief summary of each approach:
Special JS Features
The benchmark does not use any special JavaScript features like let
, const
, or ES6 syntax. It only uses basic array operations and loops.
Latest Benchmark Results
The latest results show that:
Keep in mind that the performance differences may vary depending on the specific use case and system configuration.