Vectors-function-reuse

JavaScript performance comparison

Test case created by radixzz

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function _defineProperties(t, n) {
      for (var i = 0; i < n.length; i++) {
          var r = n[i];
          r.enumerable = r.enumerable || false;
          r.configurable = true;
          if ("value" in r) r.writable = true;
          Object.defineProperty(t, r.key, r);
      }
  }
  
  function _createClass(t, n, i) {
      if (n) _defineProperties(t.prototype, n);
      if (i) _defineProperties(t, i);
      return t;
  }
  
  var t = Math.max, n = Math.min, i = Math.ceil, r = Math.floor, e = Math.round;
  
  function set(t, n, i, r, e) {
      t.x = n || 0;
      t.y = i || 0;
      t.z = r || 0;
      t.w = e || 0;
      return t;
  }
  
  function add(t, n, i) {
      t.x += n.x;
      t.y += n.y;
      if (i > 2) {
          t.z += n.z;
          if (i > 3) {
              t.w += n.w;
          }
      }
      return t;
  }
  
  function sub(t, n, i) {
      t.x -= n.x;
      t.y -= n.y;
      if (i > 2) {
          t.z -= n.z;
          if (i > 3) {
              t.w -= n.w;
          }
      }
      return t;
  }
  
  function mul(t, n, i) {
      t.x *= n.x;
      t.y *= n.y;
      if (i > 2) {
          t.z *= n.z;
          if (i > 3) {
              t.w *= n.w;
          }
      }
      return t;
  }
  
  function div(t, n, i) {
      t.x /= n.x;
      t.y /= n.y;
      if (i > 2) {
          t.z /= n.z;
          if (i > 3) {
              t.w /= n.w;
          }
      }
      return t;
  }
  
  function addScalar(t, n, i) {
      t.x += n;
      t.y += n;
      if (i > 2) {
          t.z += n;
          if (i > 3) {
              t.w += n;
          }
      }
      return t;
  }
  
  function subScalar(t, n, i) {
      t.x -= n;
      t.y -= n;
      if (i > 2) {
          t.z -= n;
          if (i > 3) {
              t.w -= n;
          }
      }
      return t;
  }
  
  function mulScalar(t, n, i) {
      t.x *= n;
      t.y *= n;
      if (i > 2) {
          t.z *= n;
          if (i > 3) {
              t.w *= n;
          }
      }
      return t;
  }
  
  function divScalar(t, n, i) {
      mulScalar(t, 1 / n, i);
      return this;
  }
  
  function min(t, i, r) {
      t.x = n(t.x, i.x);
      t.y = n(t.y, i.y);
      if (r > 2) {
          t.z = n(t.z, i.z);
          if (r > 3) {
              t.w = n(t.w, i.w);
          }
      }
      return t;
  }
  
  function max(n, i, r) {
      n.x = t(n.x, i.x);
      n.y = t(n.y, i.y);
      if (r > 2) {
          n.z = t(n.z, i.z);
          if (r > 3) {
              n.w = t(n.w, i.w);
          }
      }
      return n;
  }
  
  function clamp(i, r, e, u) {
      i.x = t(r.x, n(e.x, i.x));
      i.y = t(r.y, n(e.y, i.y));
      if (u > 2) {
          i.z = t(r.z, n(e.z, i.z));
          if (u > 3) {
              i.w = t(r.w, n(e.w, i.w));
          }
      }
      return i;
  }
  
  function clampScalar(i, r, e, u) {
      i.x = t(r, n(e, i.x));
      i.y = t(r, n(e, i.y));
      if (u > 2) {
          i.z = t(r, n(e, i.z));
          if (u > 3) {
              i.w = t(r, n(e, i.w));
          }
      }
      return i;
  }
  
  function floor(t, n) {
      t.x = r(t.x);
      t.y = r(t.y);
      if (n > 2) {
          t.z = r(t.z);
          if (n > 3) {
              t.w = r(t.w);
          }
      }
      return t;
  }
  
  function ceil(t, n) {
      t.x = i(t.x);
      t.y = i(t.y);
      if (n > 2) {
          t.z = i(t.z);
          if (n > 3) {
              t.w = i(t.w);
          }
      }
      return t;
  }
  
  function round(t, n) {
      t.x = e(t.x);
      t.y = e(t.y);
      if (n > 2) {
          t.z = e(t.z);
          if (n > 3) {
              t.w = e(t.w);
          }
      }
      return t;
  }
  
  function negate(t, n) {
      t.x = -t.x;
      t.y = -t.y;
      if (n > 2) {
          t.z = -t.z;
          if (n > 3) {
              t.w = -t.w;
          }
      }
  }
  
  function lerp(t, n, i) {
      t.x += (v.x - out_.x) * n;
      t.y += (v.y - out_.y) * n;
      if (i > 2) {
          t.z += (v.z - out_.z) * n;
          if (i > 3) {
              t.w += (v.w - out_.w) * n;
          }
      }
      return t;
  }
  
  function copy(t, n, i) {
      t.x = n.x;
      t.y = n.y;
      if (i > 2) {
          t.z = n.z;
          if (i > 3) {
              t.w = n.w;
          }
      }
      return t;
  }
  
  function equals(t, n, i) {
      var r = t.x === n.x && t.y === n.y;
      if (i > 2) {
          r = r && t.z === n.z;
          if (i > 3) {
              r = r && t.w === n.w;
          }
      }
      return r;
  }
  
  var Vec3Optimized = function() {
      function Vec3Optimized(t, n, i) {
          this.v = {
              x: t || 0,
              y: n || 0,
              z: i || 0,
              w: 0
          };
      }
      var t = Vec3Optimized.prototype;
      t.set = function set$1(t, n, i) {
          set(this.v, t, n, i, 0);
          return this;
      };
      t.add = function add$1(t) {
          add(this.v, t, 3);
          return this;
      };
      t.sub = function sub$1(t) {
          sub(this.v, t, 3);
          return this;
      };
      t.multiply = function multiply(t) {
          mul(this.v, t, 3);
  return this;
      };
      t.divide = function divide(t) {
          div(this.v, t, 3);
          return this;
      };
      t.addScalar = function addScalar$1(t) {
          addScalar(this.v, t, 3);
          return this;
      };
      t.subScalar = function subScalar$1(t) {
          subScalar(this.v, t, 3);
          return this;
      };
      t.multiplyScalar = function multiplyScalar(t) {
          mulScalar(this.v, t, 3);
          return this;
      };
      t.divideScalar = function divideScalar(t) {
          divScalar(this.v, t, 3);
          return this;
      };
      t.copy = function copy$1(t) {
          copy(this.v, t, 3);
          return this;
      };
      t.equals = function equals$1(t) {
          equals(this.v, t, 3);
          return this;
      };
      t.clone = function clone() {
          return new Vec3Optimized(this.v.x, this.v.y, this.v.z);
      };
      _createClass(Vec3Optimized, [ {
          key: "x",
          get: function get() {
              return this.v.x;
          },
          set: function set(t) {
              this.v.x = t;
          }
      }, {
          key: "y",
          get: function get() {
              return this.v.y;
          },
          set: function set(t) {
              this.v.y = t;
          }
      }, {
          key: "z",
          get: function get() {
              return this.v.z;
          },
          set: function set(t) {
              this.v.z = t;
          }
      } ]);
      return Vec3Optimized;
  }();
  
  var Vec2Optimized = function() {
      function Vec2Optimized(t, n) {
          this.v = {
              x: t || 0,
              y: n || 0,
              z: 0,
              w: 0
          };
      }
      var t = Vec2Optimized.prototype;
      t.set = function set$1(t, n) {
          set(this.v, t, n, 0, 0);
          return this;
      };
      t.add = function add$1(t) {
          add(this.v, t, 2);
          return this;
      };
      t.sub = function sub$1(t) {
          sub(this.v, t, 2);
          return this;
      };
      t.multiply = function multiply(t) {
          mul(this.v, t, 2);
          return this;
      };
      t.divide = function divide(t) {
          div(this.v, t, 2);
          return this;
      };
      t.addScalar = function addScalar$1(t) {
          addScalar(this.v, t, 2);
          return this;
      };
      t.subScalar = function subScalar$1(t) {
          subScalar(this.v, t, 2);
          return this;
      };
      t.mulScalar = function mulScalar$1(t) {
          mulScalar(this.v, t, 2);
          return this;
      };
      t.divScalar = function divScalar$1(t) {
          divScalar(this.v, t, 2);
          return this;
      };
      t.normalize = function normalize() {
          divScalar(this.v, this.length() || 1, 2);
          return this;
      };
      t.copy = function copy$1(t) {
          copy(this.v, t, 2);
          return this;
      };
      t.equals = function equals$1(t) {
          equals(t.v, 2);
          return this;
      };
      t.clone = function clone() {
          return new Vec2Optimized(this.v.x, this.v.y);
      };
      _createClass(Vec2Optimized, [ {
          key: "x",
          get: function get() {
              return this.v.x;
          },
          set: function set(t) {
              this.v.x = t;
          }
      }, {
          key: "y",
          get: function get() {
              return this.v.y;
          },
          set: function set(t) {
              this.v.y = t;
          }
      }, {
          key: "z",
          get: function get() {
              return this.v.z;
          },
          set: function set(t) {
              this.v.z = t;
          }
      } ]);
      return Vec2Optimized;
  }();
  
  var Vector3Original= 
  
  function() {
      function Vec3Original(x, y, z) {
          this.x = x || 0;
          this.y = y || 0;
          this.z = z || 0;
      }
      var t = Vec3Original.prototype;
      t.set = function set(t, i, r) {
          this.x = t;
          this.y = i;
          this.z = r;
          return this;
      };
      t.add = function add(t) {
          this.x += t.x;
          this.y += t.y;
          this.z += t.z;
          return this;
      };
      t.addScalar = function addScalar(t) {
          this.x += t;
          this.y += t;
          this.z += t;
          return this;
      };
      t.sub = function sub(t) {
          this.x -= t.x;
          this.y -= t.y;
          this.z -= t.z;
          return this;
      };
      t.subScalar = function subScalar(t) {
          this.x -= t;
          this.y -= t;
          this.z -= t;
          return this;
      };
      t.multiply = function multiply(t) {
          this.x *= t.x;
          this.y *= t.y;
          this.z *= t.z;
          return this;
      };
      t.divide = function divide(t) {
          this.x /= t.x;
          this.y /= t.y;
          this.z /= t.z;
          return this;
      };
      return Vec3Original;
  }();
  
  
  var Vector2Original = 
  
  function() {
      function Vector2Original(x, y) {
  this.set(x, y);
      }
      var t = Vector2Original.prototype;
      t.set = function set(t, i) {
          this.x = t || 0;
          this.y = i || 0;
          return this;
      };
      t.add = function add(t) {
          this.x += t.x;
          this.y += t.y;
          return this;
      };
      t.addScalar = function addScalar(t) {
          this.x += t;
          this.y += t;
          return this;
      };
      t.sub = function sub(t) {
          this.x -= t.x;
          this.y -= t.y;
          return this;
      };
      t.subScalar = function subScalar(t) {
          this.x -= t;
          this.y -= t;
          return this;
      };
      t.multiply = function multiply(t) {
          this.x *= t.x;
          this.y *= t.y;
          return this;
      };
      t.divide = function divide(t) {
          this.x /= t.x;
          this.y /= t.y;
          return this;
      };
      return Vector2Original;
  }();

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Vector2Original
var x = Math.random();
var y = Math.random();
var v1 = new Vector2Original(x, y);
var v2 = new Vector2Original(y, x);
var v3 = new Vector2Original(y, y);
var v4 = new Vector2Original(x, x);
v3.add(v1).multiply(v2).addScalar(2);
v4.sub(v1).multiply(v2).subScalar(1);
v1.multiply(v3).multiply(v4);
v2.multiply(v3).multiply(v4);
pending…
Vector3Original
var x = Math.random();
var y = Math.random();
var z = Math.random();
var v1 = new Vector3Original(x, y, z);
var v2 = new Vector3Original(y, x, z);
var v3 = new Vector3Original(z, y, x);
var v4 = new Vector3Original(x, z, y);
var v5 = new Vector3Original(y, x, z);
var v6 = new Vector3Original(z, y, x);
v5.set(v6.x, v6.y, v6.z).multiply(v3);
v6.set(v4.x, v4.y, v4.z).multiply(v4);
v1.add(v2).multiply(v3).addScalar(2);
v2.sub(v1).multiply(v3).subScalar(1);
v5.add(v1).multiply(v2).multiply(v3).sub(v4);
v6.add(v1).multiply(v2).multiply(v3).sub(v4);
pending…
Vec2Optimized
var x = Math.random();
var y = Math.random();
var v1 = new Vec2Optimized(x, y);
var v2 = new Vec2Optimized(y, x);
var v3 = new Vec2Optimized(y, y);
var v4 = new Vec2Optimized(x, x);
v3.add(v1).multiply(v2).addScalar(2);
v4.sub(v1).multiply(v2).subScalar(1);
v1.multiply(v3).multiply(v4);
v2.multiply(v3).multiply(v4);
pending…
Vec3Optimized
var x = Math.random();
var y = Math.random();
var z = Math.random();
var v1 = new Vec3Optimized(x, y, z);
var v2 = new Vec3Optimized(y, x, z);
var v3 = new Vec3Optimized(z, y, x);
var v4 = new Vec3Optimized(x, z, y);
var v5 = new Vec3Optimized(y, x, z);
var v6 = new Vec3Optimized(z, y, x);
v5.set(v6.x, v6.y, v6.z).multiply(v3);
v6.set(v4.x, v4.y, v4.z).multiply(v4);
v1.add(v2).multiply(v3).addScalar(2);
v2.sub(v1).multiply(v3).subScalar(1);
v5.add(v1).multiply(v2).multiply(v3).sub(v4);
v6.add(v1).multiply(v2).multiply(v3).sub(v4);
pending…

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

0 Comments