function addObj(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
return out;
};
function addArr(out, oo, a, b, ao,bo) {
out[0+oo] = a[0+ao] + b[0+bo];
out[1+oo] = a[1+ao] + b[1+bo];
out[2+oo] = a[2+ao] + b[2+bo];
};
var A = new Float32Array(300);
var B = new Float32Array(300);
var O = new Float32Array(300);
for (var i = 0;i < 1000;++i) {
for (var j=0;j< 100;++j) {
addArr(O,j*3,A,B,j*3,j*3);
}
}
var A = [];
var B = [];
var O = [];
for (var x=0;x<100;++x) {
A.push(new Float32Array(3));
B.push(new Float32Array(3));
O.push(new Float32Array(3));
}
for (var i = 0;i < 1000;++i) {
for (var j=0;j< 100;++j) {
addObj(O[j],A[j],B[j]);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
vec3 Arr | |
vec3 obj |
Test name | Executions per second |
---|---|
vec3 Arr | 291.1 Ops/sec |
vec3 obj | 273.7 Ops/sec |
Let's break down the provided JSON and explain what is tested, compared, and other considerations.
Benchmark Definition
The benchmark definition represents a JavaScript function that performs a specific task. In this case, there are two test cases:
vec3 Arr
: This test case uses arrays (Float32Array
) to store data. The addArr
function takes four arguments: out
, oo
, a
, and b
. It adds corresponding elements of the input arrays a
and b
to the output array out
.vec3 obj
: This test case uses objects (Float32Array
) as the data structure. The addObj
function takes three arguments: out
, a
, and b
. It adds corresponding properties of the input objects a
and b
to the output object out
.Options Compared
The benchmark compares two options:
vec3 Arr
): This approach uses arrays to store data, which are created using Float32Array
. The addArr
function adds elements of the input arrays to the output array.vec3 obj
): This approach uses objects as the data structure, where each object has three properties (e.g., x
, y
, and z
). The addObj
function adds corresponding properties of the input objects to the output object.Pros and Cons
Here are some pros and cons of each approach:
Array-based approach (vec3 Arr
)
Pros:
Cons:
Object-based approach (vec3 obj
)
Pros:
Cons:
Other Considerations
Float32Array
instead of native number types (e.g., number
) may incur a small overhead.Libraries and Special JS Features
In the provided benchmark definition, there is no mention of any specific libraries or special JavaScript features. However, it's worth noting that:
Float32Array
is a typed array class in JavaScript, which provides efficient memory management for floating-point numbers.push
and push
-style methods (e.g., A.push(new Float32Array(3))
) suggests using modern JavaScript features.Alternatives
Other alternatives to compare the performance of array-based and object-based approaches might include:
It's worth noting that the specific choices made in this benchmark definition (using arrays and Float32Array
) may be influenced by factors such as: