var n = 100000;
var x = [], y =[], z =[];
var x32 = new Float32Array(n);
var y32 = new Float32Array(n);
var z32 = new Float32Array(n);
var vectors = [];
for(var i = 0; i < n; i++){
x[i] = Math.random()*100|0;
y[i] = Math.random()*100|0;
z[i] = Math.random()*100|0;
x32[i] = Math.random()*100|0;
y32[i] = Math.random()*100|0;
z32[i] = Math.random()*100|0;
vectors[i] = { x: x[i], y: y[i], z: z[i] };
}
var vector;
for (var i = 0; i < n; ++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; i < n; ++i) {
x32[i] = 2 * x32[i];
y32[i] = 2 * y32[i];
z32[i] = 2 * z32[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
soa | |
aos | |
typed soa |
Test name | Executions per second |
---|---|
soa | 16.0 Ops/sec |
aos | 13.2 Ops/sec |
typed soa | 16.1 Ops/sec |
Let's break down the provided benchmark definition and individual test cases to understand what is being tested.
Benchmark Definition
The benchmark defines three different approaches:
vectors
array using a for loop with the length of the array (li
). The current vector is accessed and its elements are multiplied by 2.vectors
object, which may not be supported by all browsers or environments.x
, y
, and z
arrays using traditional for loops with indices (i
). The corresponding elements in each array are multiplied by 2.Float32Array
objects instead of traditional arrays.Float32Array
objects, which may not be supported by all browsers or environments.Test Cases
Each test case represents a separate instance of the benchmark, testing one of the three approaches. The execution times are recorded for each test case.
Library/Functionality Used
None of the test cases explicitly use any external libraries or functions beyond the standard JavaScript built-in functionality.
Special JS Feature/Syntax (None)
There is no explicit mention of special JavaScript features or syntax being used in the benchmark definition or individual test cases.
Alternative Approaches
Other possible approaches to this benchmark could include:
for...of
loops, which might provide better performance and efficiency.Keep in mind that the specific approach chosen ultimately depends on the goals of the benchmark, the target audience, and the desired outcome.