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,j*3,A,B,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 | 132.5 Ops/sec |
vec3 obj | 110.0 Ops/sec |
vec3 arr2 | 0.3 Ops/sec |
Overview of the Benchmark
The provided benchmark measures the performance of three different approaches for matrix multiplication in JavaScript, specifically targeting web browsers.
Benchmark Definition JSON
The benchmark definition is an object that contains information about the test case:
Name
: The name of the test case ("matrix test")Description
: A brief description of the test caseScript Preparation Code
: The JavaScript code required to prepare for the test case. This includes two custom functions, addObj
and addArr
, which perform matrix multiplication on 1D arrays (Float32Array
) or objects.Html Preparation Code
: An empty string, indicating that no HTML preparation is needed.Individual Test Cases
There are three test cases:
addArr
function to multiply two 2D matrices represented as 1D arrays (Float32Array
) and stores the result in a third 1D array (Float32Array
). The matrices have dimensions (100, 100) and are multiplied by performing nested loops.addObj
function to multiply two 2D objects, where each element is an object with three float32 values representing a row in the matrix. The matrices have dimensions (100, 100) and are multiplied using a similar approach to vec3 Arr.addArr2
function, which is similar to addArr
, but allows for optional padding of rows and columns. The matrices have the same dimensions as in vec3 Arr.Library Used
None of the benchmark definitions explicitly use a library like NumJS or MathJS, although they do utilize native JavaScript arrays (Float32Array
) and objects. However, some libraries may provide similar functionality to these custom functions.
Special JS Features/Syntax
The benchmark uses:
var A = function() { ... }
)var B = new Float32Array(300)
)for
loops)These features are standard in modern JavaScript and do not require special knowledge.
Performance Comparison
The benchmark compares the performance of three different approaches:
addArr
function to multiply matrices represented as 1D arrays.addObj
function to multiply matrices represented as objects with float32 values.addArr2
function, which allows for optional padding of rows and columns.The performance results are reported in executions per second (ExecutionsPerSecond).
Pros and Cons
Each approach has its pros and cons:
Alternatives
Some alternative approaches could be:
However, the benchmark is focused on measuring JavaScript performance in web browsers, so these alternative approaches may not be directly applicable.