Vector Maths

JavaScript performance comparison

Test case created by martensms

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;

        };

        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>
<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.

Testing in unknown unknown
Test Ops/sec
vec3.create()
for (var i = 0, l = 100000; i < l; i++) {
  _instances.push(vec3.create());
}
pending…
new vec3_proto()
for (var i = 0, l = 100000; i < l; i++) {
  _instances.push(new vec3_proto());
}
pending…
vec3.set()
for (var i = 0, l = 100000; i < l; i++) {
  _instances.push(vec3.create());
  vec3.set(_instances[i],1,2,3);
}
pending…
vec3_proto.set()
for (var i = 0, l = 100000; i < l; i++) {
  _instances.push(new vec3_proto());
  _instances[i].set(1,2,3);
}
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