new Vec3 vs duck

JavaScript performance comparison

Revision 2 of this test case created by Pipo

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function ThisVec3(x, y, z){
      this.x = x;
      this.y = y;
      this.z = z;
    }
   
    function ObjVec3(x, y, z){
      return {
        x: x,
        y: y,
        z: z
      }
    }
   
    function ArrVec3(x, y, z){
      return [x, y, z]
    }
   
    function ProtoVec3(x, y, z){
      this.x = x;
      this.y = y;
      this.z = z;
    }
   
    ProtoVec3.prototype.add = function( vec ){
      this.x += vec.x;
      this.y += vec.y;
      this.z += vec.z;
    }
   
    var vec3add = function( dest, other ){
      dest.x += other.x;
      dest.y += other.y;
      dest.z += other.z;
      return dest;
    }
   
    var vec3addArr = function( dest, other ){
      dest[0] += other[0];
      dest[1] += other[1];
      dest[2] += other[2];
      return dest;
    }
};
</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 using 'this'
var v1 = new ThisVec3( Math.random(), Math.random(), Math.random() );
var v2 = new ThisVec3( Math.random(), Math.random(), Math.random() );
vec3add( v1, v2 );
v1.x;
v1.y;
v1.z;
 
pending…
Vec3 using factory
var v1 = ObjVec3( Math.random(), Math.random(), Math.random() );
var v2 = ObjVec3( Math.random(), Math.random(), Math.random() );
vec3add( v1, v2 );
v1.x;
v1.y;
v1.z;
 
pending…
Vec3 with prototype
var v1 = new ProtoVec3( Math.random(), Math.random(), Math.random() );
var v2 = new ProtoVec3( Math.random(), Math.random(), Math.random() );
v1.add( v2 );
v1.x;
v1.y;
v1.z;
pending…
Vec3 using Array factory
var v1 = ArrVec3( Math.random(), Math.random(), Math.random() );
var v2 = ArrVec3( Math.random(), Math.random(), Math.random() );
vec3addArr( v1, v2 );
v1.x;
v1.y;
v1.z;
 
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