Skip to content

Instantly share code, notes, and snippets.

@nickbalestra
Last active August 29, 2015 14:24
Show Gist options
  • Save nickbalestra/6b9a19f5532aedbbf80e to your computer and use it in GitHub Desktop.
Save nickbalestra/6b9a19f5532aedbbf80e to your computer and use it in GitHub Desktop.
Istantiation Patterns - Performance Test
var Functional = function(){
var istance = {};
var private = {};
istance.firstMethod = function(){};
istance.secondMethod = function(){};
istance.thirdMethods = function(){};
istance.fourthMethod = function(){};
istance.fifthMethod = function(){};
istance.lastMethod = function(){};
return istance;
};
var FunctionalShared = function(){
var istance = {};
istance._private = {};
extend(istance, functionalSharedMethods);
return istance;
};
var functionalSharedMethods = {
firstMethod: function(){},
secondMethod: function(){},
thirdMethods: function(){},
fourthMethod: function(){},
fifthMethod: function(){},
lastMethod: function(){}
};
// Basic Extend Helper
function extend(istance, methods) {
for (var key in methods) {
if(!istance.hasOwnProperty(key)){
istance[key] = methods[key];
}
}
}
<html>
<head>
<meta charset="UTF-8">
<title>Istantation Patterns Permormance Test</title>
<script src="functional.js"></script>
<script src="functionalShared.js"></script>
<script src="protototypal.js"></script>
<script src="pseudoclassical.js"></script>
</head>
<body>
<script>
function test(num, instantiationPattern, name){
var mock;
var start = window.performance.now();
for (var i = 0; i < num; i++) {
if (name === "pseudoclassical"){
mock = new instantiationPattern();
} else {
mock = instantiationPattern();
}
}
var end = window.performance.now();
var elapsed = end - start;
console.log("Created " + num + " " + name + " istances in " + elapsed + " milliseconds");
}
test(1000000, Functional, "functional");
test(1000000, FunctionalShared, "functionalShared");
test(1000000, Prototypal, "prototypal");
test(1000000, Pseudoclassical, "pseudoclassical");
</script>
</body>
</html>
var Prototypal = function(){
var istance = Object.create(protototypalMethods);
istance._private = {};
return istance;
}
var protototypalMethods = {
firstMethod: function(){},
secondMethod: function(){},
thirdMethods: function(){},
fourthMethod: function(){},
fifthMethod: function(){},
lastMethod: function(){}
};
var Pseudoclassical = function(){
this._private = {};
this.firstMethod = function(){};
this.secondMethod = function(){};
this.thirdMethods = function(){};
this.fourthMethod = function(){};
this.fifthMethod = function(){};
this.lastMethod = function(){};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment