Module Pattern vs Object Literal vs Prototype

JavaScript performance comparison

Revision 8 of this test case created by Rafa

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  /*Módule Pattern*/
  var MyModulePattern = (function() {
    //Provate Properties
    var property1;
    var property2;
  
    //Private Methods
    var ConcatProperties = function() {
      return property1 + property2;
    }
  
    //Public Methods
    return {
      GetProperty1: function() {
        return property1;
      },
  
      GetProperty2: function() {
        return property2;
      },
      SetProperty1: function(value) {
        property1 = value;
      },
  
      SetProperty2: function(value) {
        property2 = value;
      },
  
      GetProperties: function() {
        return ConcatProperties();
      }
    }
  }());
  
  /*Object Literal Notation*/
  var MyObjectLiteralNotation = {
  	property1: undefined,
  	property2: undefined,
  	
  	ConcatProperties: function () {
  		return property1 + property2;
  	},
  
  	GetProperty1: function () {
  		return property1;
  	},
  
  	GetProperty2: function () {
  		return property2;
  	},
  	SetProperty1: function (value) {
  		property1 = value;
  	},
  
  	SetProperty2: function (value) {
  		property2 = value;
  	},
  	
  	GetProperties: function() {
  		return this.ConcatProperties(); 
  	}
  };
  
  /*Prototype*/
  var MyModulePrototype = function () {
  	var property1;
  	var property2;
  	
  	//Private Methods
  	this.ConcatProperties = function () {
  		return this.property1 + this.property2;
  	}
  };
  
  MyModulePrototype.prototype.GetProperty1 = function () {
  			return this.property1;
  		};
  
  MyModulePrototype.prototype.GetProperty2 = function () {
  			return this.property2;
  		};
  
  MyModulePrototype.prototype.SetProperty1 = function (value) {
  			this.property1 = value;
  		};
  
  MyModulePrototype.prototype.SetProperty2 = function (value) {
  			this.property2 = value;
  		};
  
  MyModulePrototype.prototype.GetProperties = function() {
  			return this.ConcatProperties(); 
  		};	
  
  var oMyModulePrototype = new MyModulePrototype();
  
  
  /*Closure*/
  /*Closure*/
  var MyClosureModule = (function () {
      var MyModuleConstructor = function () {
          var property1;
          var property2;
  
          //Private Methods
          function ConcatProperties() {
              return property1 + property2;
          }
  
          function GetProperty1 () {
              return property1;
          }
  
          function GetProperty2 () {
              return property2;
          }
  
          function SetProperty1 (value) {
              property1 = value;
          }
  
          function SetProperty2 (value) {
              property2 = value;
          }
  
          function GetProperties() {
              return ConcatProperties();
          }
  
          return {
              property1: undefined,
              property1: undefined,
              GetProperty1,
              GetProperty2,
              SetProperty1,
              SetProperty2,
              GetProperties
          };
      };
  
      
  })();
  
  var oMyClosureModule = MyClosureModule();

};
</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
Closure
oMyClosureModule.SetProperty1("Prop1");
oMyClosureModule.SetProperty2("Prop2");
oMyClosureModule.GetProperty1();
oMyClosureModule.GetProperty2();
oMyClosureModule.GetProperties();
pending…

Revisions

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

0 Comments