Module Pattern vs Object Literal vs Prototype
JavaScript performance comparison
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.
Test | Ops/sec | |
---|---|---|
Module Pattern
|
|
pending… |
Object Literal Notation
|
|
pending… |
Prototype
|
|
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.
- Revision 1: published Jorge Castro
- Revision 2: published Jorge Castro
- Revision 3: published Jorge Castro
- Revision 4: published Jorge Castro
- Revision 5: published Digitale Welten
- Revision 6: published Digitale Welten
0 Comments