Module Pattern vs Object Literal vs Prototype

JavaScript performance comparison

Test case created by Jorge Castro

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 MyModuleObjectLiteralNotation = (function() {
    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();
      }
    }
  });
  
  var oMyModuleObjectLiteralNotation = new MyModuleObjectLiteralNotation();
  
  /*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();

};
</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
Module Pattern
MyModulePattern.SetProperty1("Prop1");
MyModulePattern.SetProperty2("Prop2");
MyModulePattern.GetProperty1();
MyModulePattern.GetProperty2();
MyModulePattern.GetProperties();
pending…
Object Literal Notation
oMyModuleObjectLiteralNotation.SetProperty1("Prop1");
oMyModuleObjectLiteralNotation.SetProperty2("Prop2");
oMyModuleObjectLiteralNotation.GetProperty1();
oMyModuleObjectLiteralNotation.GetProperty2();
oMyModuleObjectLiteralNotation.GetProperties();
pending…
Prototype
oMyModulePrototype.SetProperty1("Prop1");
oMyModulePrototype.SetProperty2("Prop2");
oMyModulePrototype.GetProperty1();
oMyModulePrototype.GetProperty2();
oMyModulePrototype.GetProperties();
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