diff in speed for three.js (v4)

Revision 4 of this benchmark created on


Preparation HTML

<script src="https://github.com/mrdoob/three.js/raw/master/build/Three.js"></script>
<script src="https://github.com/mrdoob/three.js/raw/master/src/core/Matrix4.js"></script>

<script src="https://github.com/mrdoob/three.js/raw/master/src/core/Vector3.js"></script>
<script>
  THREE.Matrix4.prototype.transformVector3_2 = function(v) {
  
   var vx = v.x,
       vy = v.y,
       vz = v.z,
       d = 1 / (this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44);
  
   v.x = (this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14) * d;
   v.y = (this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24) * d;
   v.z = (this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34) * d;
  
   return v;
  
  };
  
  THREE.Matrix4.prototype.transformVector3_3 = function(v) {
  
   var vx = v.x,
       vy = v.y,
       vz = v.z,
       d = 1 / (this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44);
  
   if (d == 1) {
    v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14;
    v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24;
    v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34;
   } else {
    v.x = (this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14) * d;
    v.y = (this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24) * d;
    v.z = (this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34) * d;
   };
   return v;
  
  };
  
  var a = new THREE.Matrix4();
  var b = new THREE.Vector3(1, 1, 1);
  var c = new THREE.Matrix4();
  c.n44 = 1.1;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
normal
b = new THREE.Vector3(1, 1, 1);
a.transformVector3(b);
ready
improved
b = new THREE.Vector3(1, 1, 1);
a.transformVector3_2(b);
ready
test if improved
b = new THREE.Vector3(1, 1, 1);
a.transformVector3_3(b);
ready
test if improved with d not 1 (shouldn't occur normally)
b = new THREE.Vector3(1, 1, 1);
c.transformVector3_3(b);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.