Vector Maths
JavaScript performance comparison
Info
Performance comparison between instance based Vector maths and C-style module library. Determination of speed difference due to prototype lookup.
Preparation code
<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;
};
Module.add = function(result, a, b) {
result[0] = a[0] + b[0];
result[1] = a[1] + b[1];
result[2] = a[2] + b[2];
};
return Module;
})();
var vec3_proto = (function() {
var _type = typeof Float32Array !== 'undefined' ? Float32Array : Array;
var Class = function() {
this.__data = new _type(3);
};
Class.prototype.clone = function(vector) {
var clone = new Class();
clone.set(this.__data[0], this.__data[1], this.__data[2]);
return clone;
};
Class.prototype.copy = function(copy) {
copy.__data[0] = this.__data[0];
copy.__data[1] = this.__data[1];
copy.__data[2] = this.__data[2];
};
Class.prototype.set = function(x, y, z) {
this.__data[0] = x;
this.__data[1] = y;
this.__data[2] = z;
};
Class.prototype.add = function(a) {
this.__data[0] += a.__data[0];
this.__data[1] += a.__data[1];
this.__data[2] += a.__data[2];
};
return Class;
})();
</script>
<script>
Benchmark.prototype.setup = function() {
var _instances = [];
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
vec3.create() |
|
pending… |
new vec3_proto() |
|
pending… |
vec3.set() |
|
pending… |
new vec3_proto.set() |
|
pending… |
vec3.add(result, a,b); |
|
pending… |
new vec3_proto().add(a); |
|
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:
- Revision 1: published by martensms
- Revision 2: published
0 comments