Editing vec3 foreach This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Testing performance of a "foreach" concept for glMatrix 2.0 Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) 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.addVecs = function(vecs,v2){ 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]; } } 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]); Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code 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]; } Test 2 Title Async (check if this is an asynchronous test) Code 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]; } Test 3 Title Async (check if this is an asynchronous test) Code vec3.forEach(vecs, 3, 0, 0, vec3.add, v2); Test 4 Title Async (check if this is an asynchronous test) Code vec3.addVecs(vecs,v2);