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) {
x[i] = 2 * x[i];
}
for (var i = 0, li=y.length; i < li; ++i) {
y[i] = 2 * y[i];
}
for (var i = 0, li=z.length; i < li; ++i) {
z[i] = 2 * z[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
soa | |
aos | |
soa mark II |
Test name | Executions per second |
---|---|
soa | 92.0 Ops/sec |
aos | 6.3 Ops/sec |
soa mark II | 95.4 Ops/sec |
I'll break down the test cases and explain what's being tested.
Benchmark Definition
The benchmark is comparing two approaches to scaling an array of numbers:
Library
The Float32Array
is used to represent vectors. It's a typed array object that provides efficient storage for floating-point numbers.
Special JS Feature/Syntax
None of the benchmark tests use special JavaScript features or syntax, such as async/await, promises, or modern web APIs like Web Workers or Web Assembly.
Test Cases
The test cases are:
Performance
The benchmark results show that the "soa" approach is significantly faster than both "aos" approaches, with an execution rate of approximately 2.03 executions per second for "soa mark II" and 1.3798 executions per second for "aos". This suggests that the first approach is more efficient due to the reduced number of memory accesses.
Other Alternatives
Alternative approaches could include:
Keep in mind that the performance results will depend on the specific use case and hardware configuration.