examining typeof(parameter) vs using arguments[] in constructor

Benchmark created on


Description

examining typeof(parameter) vs using arguments[] in constructor

Preparation HTML

<script>
  Point1 = function (x, y) {
      if (typeof (x) === 'number') {
          this.x = x || 0;
          this.y = y || 0;
      } else if (typeof (x) === 'object') {
          this.x = x[0] || x.x || 0;
          this.y = x[0] || x.y || 0;
      } else {
          this.x = 0;
          this.y = 0;
      }
  };
  
  Point2 = function (x, y) {
      if (arguments.length === 2) {
        this.x = arguments[0] || 0;
        this.y = arguments[1] || 0;
      } else {
        this.x  = arguments[0][0] || arguments[0].x || 0;
        this.x  = arguments[0][1] || arguments[0].y || 0;
      }    
  };
  
  Point3 =  function (x, y) {
    this.x = x || 0; 
    this.y = y || 0;
  }
  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
examing param types
var p1 = new Point1(1,1);
var p2 = new Point1([2,2]);
var P3 = new Point1(p1);
ready
examine arguments object
var p1 = new Point2(1,1);
var p2 = new Point2([2,2]);
var P3 = new Point2(p1);
ready

Revisions

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