vec3 foreach

JavaScript performance comparison

Test case created by Brandon Jones

Info

Testing performance of a "foreach" concept for glMatrix 2.0

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var vec3 = {};
   
    vec3.add = function(out, a, b) {
        out[0] = a[0] + b[0];
        out[1] = a[1] + b[1];
        out[2] = a[2] + b[2];
        return out;
    };
   
    vec3.forEach = (function() {
        var vec = new Float32Array(3);
   
        return function(a, stride, offset, count, fn, arg) {
            var i, l;
            if(!stride) {
                stride = 3;
            }
           
            if(count) {
                l = Math.min(count * stride, a.length - offset);
            } else {
                l = a.length - offset;
            }
   
            for(i = offset; i < l; i += stride) {
                vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2];
                fn(vec, vec, arg);
                a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2];
            }
           
            return a;
        };
    })();
   
    var vecs = new Float32Array(3000);
    var v1 = new Float32Array([0, 0, 0]);
    var v2 = new Float32Array([5, 5, 5]);
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Manual Loop
var i, l = vecs.length;
for(i = 0; i < l; i += 3) {
  v1[0] = vecs[i]; v1[1] = vecs[i+1]; v1[2] = vecs[i+2];
  vec3.add(v1, v1, v2);
  vecs[i] = v1[0]; vecs[i+1] = v1[1]; vecs[i+2] = v1[2];
}
pending…
Optimized Loop
var i, l = vecs.length;
for(i = 0; i < l; i += 3) {
  vecs[i] += v2[0];
  vecs[i+1] += v2[1];
  vecs[i+2] += v2[2];
}
pending…
ForEach loop
vec3.forEach(vecs, 3, 0, 0, vec3.add, v2);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment