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];
};
function addArr2(out, a, b, oo, ao,bo) {
out[0+(oo || 0)] = a[0+(ao || 0)] + b[0+(bo || 0)];
out[1+(oo || 0)] = a[1+(ao || 0)] + b[1+(bo || 0)];
out[2+ (oo || 0)] = a[2+(ao || 0)] + b[2+(bo || 0)];
};
var A = new Float32Array(300);
var B = new Float32Array(300);
var O = new Float32Array(300);
for (var i = 0;i < 10000;++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 < 10000;++i) {
for (var j=0;j< 100;++j) {
addObj(O[j],A[j],B[j]);
}
}
var A = new Float32Array(300);
var B = new Float32Array(300);
var O = new Float32Array(300);
for (var i = 0;i < 10000;++i) {
for (var j=0;j< 100;++j) {
addArr2(O,A,B,j * 3,j*3,j*3);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
vec3 Arr | |
vec3 obj | |
vec3 arr2 |
Test name | Executions per second |
---|---|
vec3 Arr | 131.4 Ops/sec |
vec3 obj | 110.5 Ops/sec |
vec3 arr2 | 75.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition JSON
The provided JSON represents a benchmark definition, which is a set of instructions that outline how to create and execute the benchmark. In this case, we have three test cases: vec3 Arr
, vec3 obj
, and vec3 arr2
. Each test case has its own script preparation code, which defines functions for adding elements to arrays (addObj
and addArr
) or using a default function for adding elements (addArr2
).
Options Compared
The three test cases compare different approaches for adding elements to arrays:
vec3 Arr
: Uses the push
method to add new arrays to an array, followed by calling addObj
on each element.vec3 obj
: Creates a new array with initial values using new Float32Array(3)
, and then pushes this array into another array. Finally, it calls addObj
on each element of the nested array.vec3 arr2
: Uses the default function for adding elements to arrays (addArr2
) to add elements directly to an existing array.Pros and Cons
push
.Other Considerations
Library Used
None of the test cases explicitly use any external libraries beyond the built-in Float32Array
type.
Special JS Features or Syntax
None of the test cases rely on special JavaScript features or syntax beyond the standard language specification.