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) {
xt[i] = 2 * xt[i];
yt[i] = 2 * yt[i];
zt[i] = 2 * zt[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=xt.length; i < li; ++i) {
xt[i] = 2 * xt[i];
}
for (var i = 0, li=yt.length; i < li; ++i) {
yt[i] = 2 * yt[i];
}
for (var i = 0, li=zt.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 | 74.3 Ops/sec |
aos | 6.4 Ops/sec |
soa mark II | 69.5 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark test case, specifically comparing two approaches to vector operations: Staged Operations (SOA) and Array Operations (AOs).
What is being tested?
In this benchmark, two sets of vectors are created: xt
, yt
, and zt
are 1,000,000-element Float32Array arrays, while x
, y
, and z
are regular JavaScript arrays. The test case then performs a series of operations on each array:
xt[i] = 2 * xt[i];
yt[i] = 2 * yt[i];
zt[i] = 2 * zt[i];
vector = vectors[i];
vector.x = 2 * vector.x;
vector.y = 2 * vector.y;
vector.z = 2 * vector.z;
Options compared
Two main options are being compared:
Pros and Cons
Pros of SOA:
Cons of SOA:
Pros of AOs:
Cons of AOs:
Library/Function usage
No specific libraries or functions are used in this benchmark, only built-in JavaScript features.
Special JS feature/syntax
The benchmark uses the for...in
loop (not shown explicitly) to iterate over the array elements, which is a legacy syntax. However, it does not use any modern ES6+ features like for...of
, forEach
, or arrow functions.
Alternative approaches
Other alternatives for vector operations in JavaScript include:
Keep in mind that the best approach will depend on the specific requirements and constraints of your application.