# vec3 foreach

## JavaScript performance comparison

Revision 3 of this test case created by

## Preparation code

``````<script>
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;
};

var i, l = vecs.length;
for(i = 0; i <= (l-3); i += 3) {
vecs[i] += v2[0];
vecs[i+1] += v2[1];
vecs[i+2] += v2[2];
}
}
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 - stride); 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;
};
})();
</script>

<script>
Benchmark.prototype.setup = function() {
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 CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Manual Loop
``````var i, l = vecs.length;
for(i = 0; i <= (l-3); i += 3) {
v1[0] = vecs[i]; v1[1] = vecs[i+1]; v1[2] = vecs[i+2];
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 - 3); 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…
``vec3.addVecs(vecs,v2);``
You can edit these tests or add even more tests to this page by appending `/edit` to the URL.