Editing Vector Maths 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) Performance comparison between instance based Vector maths and C-style module library. Determination of speed difference due to prototype lookup. 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) <script> var vec3 = (function() { var _type = typeof Float32Array !== 'undefined' ? Float32Array : Array; var Module = {}; Module.create = function() { var vector = new _type(3); vector[0] = 0; vector[1] = 0; vector[2] = 0; return vector; }; Module.clone = function(vector) { var clone = new _type(3); clone[0] = vector[0]; clone[1] = vector[1]; clone[2] = vector[2]; return clone; }; Module.copy = function(copy, origin) { copy[0] = origin[0]; copy[1] = origin[1]; copy[2] = origin[2]; }; Module.set = function(vector, x, y, z) { vector[0] = x; vector[1] = y; vector[2] = z; }; return Module; })(); var vec3_proto = (function() { var _type = typeof Float32Array !== 'undefined' ? Float32Array : Array; var Class = function() { var vector = new _type(3); vector[0] = 0; vector[1] = 0; vector[2] = 0; vector.prototype = Class.prototype; return vector; }; Class.prototype.clone = function() { var clone = new Class(); clone[0] = this[0]; clone[1] = this[1]; clone[2] = this[2]; return clone; }; Class.prototype.copy = function(copy) { copy[0] = this[0]; copy[1] = this[1]; copy[2] = this[2]; }; Class.prototype.set = function(x, y, z) { this[0] = x; this[1] = y; this[2] = z; }; return Class; })(); </script> 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 _instances = []; 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 for (var i = 0, l = 100000; i < l; i++) { _instances.push(vec3.create()); } Test 2 Title Async (check if this is an asynchronous test) Code for (var i = 0, l = 100000; i < l; i++) { _instances.push(new vec3_proto()); } Test 3 Title Async (check if this is an asynchronous test) Code for (var i = 0, l = 100000; i < l; i++) { _instances.push(vec3.create()); vec3.set(_instances[i],1,2,3); } Test 4 Title Async (check if this is an asynchronous test) Code for (var i = 0, l = 100000; i < l; i++) { _instances.push(new vec3_proto()); _instances[i].set(1,2,3); }