Methods in a constructor vs prototype

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function ProtoMethods (x,y,z) {
      this.x=x;
      this.y=y;
      this.z=z;
  }
  
  ProtoMethods.prototype.method1 = function (x) {
      this.x=x;
  };
  
  ProtoMethods.prototype.method2 = function (y) {
      this.y=y;
  };
  
  ProtoMethods.prototype.method3 = function (z) {
      this.z=z;
  };
  
  function CtorMethodsPrivate (x,y,z) {
      var x,y,z;
      
      this.method1 = function (x) {
          x=x;
      };
  
      this.method2 = function (y) {
          y=y;
      };
  
      this.method3 = function (z) {
          z=z;
      };
  }
  
  function CtorMethodsPublic (x,y,z) {
      this.x=x;
      this.y=y;
      this.z=z;
  
      this.method1 = function (x) {
          this.x=x;
      };
  
      this.method2 = function (y) {
          this.y=y;
      };
  
      this.method3 = function (z) {
          this.z=z;
      };
  }
  
  function ProtoMethodsPrivateStatic (x,y,z) {
      this.method1(x);
      this.method2(y);
      this.method3(z);
  }
  
  (function () {
      var x,y,z;
  
      ProtoMethodsPrivateStatic.prototype.method1 = function (x) {
          x=x;
      };
  
      ProtoMethodsPrivateStatic.prototype.method2 = function (y) {
          y=y;
      };
  
      ProtoMethodsPrivateStatic.prototype.method3 = function (z) {
          z=z;
      };
  })();
  
  function ProtoMethodsPrivate () {
  }
  
  function NewProtoMethodsPrivate (x,y,z) {
      var x=x,y=y,z=z;
      
      ProtoMethodsPrivate.prototype.method1 = function (arg) {
          x=arg;
      };
      
      ProtoMethodsPrivate.prototype.method2 = function (arg) {
          y=arg;
      };
      
      ProtoMethodsPrivate.prototype.method3 = function (arg) {
          z=arg;
      };
  
      return new ProtoMethodsPrivate();
  }
  
  function ProtoMethodsPrivateECMA5(x,y,z) {
      var x,y,z;
      
      Object.defineProperties(this, {
          x: { get: function () { return x; },
               set: function (arg) { x = arg; }
          },
          y: { get: function () { return y; },
               set: function (arg) { y = arg; }
          },
          x: { get: function () { return z; },
               set: function (arg) { z = arg; }
          }
      });
  }
  
  ProtoMethodsPrivateECMA5.prototype.method1 = function (x) {
      this.x=x;
  };
  
  ProtoMethodsPrivateECMA5.prototype.method2 = function (y) {
      this.y=y;
  };
  
  ProtoMethodsPrivateECMA5.prototype.method3 = function (z) {
      this.z=z;
  };
  
  var instance;

};
</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
Prototype Methods and Public Properties
instance = new ProtoMethods(1,2,3);
instance.method1(3);
instance.method2(2);
instance.method3(1);
pending…
Constructor Methods and Private Properties
instance = new CtorMethodsPrivate(1,2,3);
instance.method1(3);
instance.method2(2);
instance.method3(1);
pending…
Constructor Methods and Public Properties
instance = new CtorMethodsPublic(1,2,3);
instance.method1(3);
instance.method2(2);
instance.method3(1);
pending…
Prototype Methods Private Variables via Closure
instance = NewProtoMethodsPrivate(1,2,3);
instance.method1(3);
instance.method2(2);
instance.method3(1);
pending…
Prototype Methods, Private Static Properties
instance = new ProtoMethodsPrivateStatic(1,2,3);
instance.method1(3);
instance.method2(2);
instance.method3(1);
pending…
Prototype Methods, Private Properties via Constructor and Getter/Setter
instance = new ProtoMethodsPrivateECMA5(1,2,3);
instance.method1(3);
instance.method2(2);
instance.method3(1);
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.

0 Comments