Module Pattern vs Object Literal vs Prototype

JavaScript performance comparison

Revision 5 of this test case created by Digitale Welten

Preparation code

<script>
/*Classic Module Pattern*/
var MyClassicModulePattern = (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: null,
  property2: null,

  ConcatProperties: function() {
    return this.property1 + this.property2;
  },

  GetProperty1: function() {
    return this.property1;
  },

  GetProperty2: function() {
    return this.property2;
  },
  SetProperty1: function(value) {
    this.property1 = value;
  },

  SetProperty2: function(value) {
    this.property2 = value;
  },

  GetProperties: function() {
    return this.ConcatProperties();
  }
};

/*Module with individual Prototype*/
function MyModuleIndividualPrototype(name) {
  this.name = name;
  this.property1 = null;
  this.property2 = null;
}

MyModuleIndividualPrototype.prototype.ConcatProperties = function() {
  return this.property1 + this.property2;
};

MyModuleIndividualPrototype.prototype.GetProperty1 = function() {
  return this.property1;
};

MyModuleIndividualPrototype.prototype.GetProperty2 = function() {
  return this.property2;
};

MyModuleIndividualPrototype.prototype.SetProperty1 = function(value) {
  this.property1 = value;
};

MyModuleIndividualPrototype.prototype.SetProperty2 = function(value) {
  this.property2 = value;
};

MyModuleIndividualPrototype.prototype.GetProperties = function() {
  return this.ConcatProperties();
};

var oMyModuleIndividualPrototype = new MyModuleIndividualPrototype('foo');

/* Module override Prototype*/
var MyModulePatternOverridePrototype = (function() {
  //Private Properties
  var property1;
  var property2;

  // constructor
  var MyModulePatternOverridePrototype = function() {};

  //Private Methods
  var ConcatProperties = function() {
    return property1 + property2;
  }

  // prototype
  MyModulePatternOverridePrototype.prototype = {
    constructor: MyModulePatternOverridePrototype,
    GetProperty1: function() {
      return property1;
    },
    GetProperty2: function() {
      return property2;
    },
    SetProperty1: function(value) {
      property1 = value;
    },
    SetProperty2: function(value) {
      property2 = value;
    },
    GetProperties: function() {
      return ConcatProperties();
    }
  };

  return MyModulePatternOverridePrototype;
})();

var oMyModulePatternOverridePrototype = new MyModulePatternOverridePrototype();
</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
Classic Module Pattern
MyClassicModulePattern.SetProperty1("Prop1");
MyClassicModulePattern.SetProperty2("Prop2");
MyClassicModulePattern.GetProperty1();
MyClassicModulePattern.GetProperty2();
MyClassicModulePattern.GetProperties();
pending…
Object Literal Notation
MyObjectLiteralNotation.SetProperty1("Prop1");
MyObjectLiteralNotation.SetProperty2("Prop2");
MyObjectLiteralNotation.GetProperty1();
MyObjectLiteralNotation.GetProperty2();
MyObjectLiteralNotation.GetProperties();
pending…
Module With Individual Prototype
oMyModuleIndividualPrototype.SetProperty1("Prop1");
oMyModuleIndividualPrototype.SetProperty2("Prop2");
oMyModuleIndividualPrototype.GetProperty1();
oMyModuleIndividualPrototype.GetProperty2();
oMyModuleIndividualPrototype.GetProperties();
pending…
Module Pattern override Prototype
oMyModulePatternOverridePrototype.SetProperty1("Prop1");
oMyModulePatternOverridePrototype.SetProperty2("Prop2");
oMyModulePatternOverridePrototype.GetProperty1();
oMyModulePatternOverridePrototype.GetProperty2();
oMyModulePatternOverridePrototype.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