Creational Patterns to Test
I tested the three most popular creational patterns in JavaScript. Object literals, functional objects, and pseudoclassical objects. In particular, I wanted to test their singleton and instance based execution times./*
* Pseudoclassical object designed as a singleton
*/
var PseudoclassicalSingleton = new function(){
this.getName = function(){
return 'Pseudoclassical';
};
};
/*
* Functional object designed as a singleton
*/
var FunctionalSingleton = (function(){
return {
getName: function(){
return 'Functional';
}
};
})();
/*
* Object literal (implicitly singleton)
*/
var ObjectLiteralSingleton = {
getName: function(){
return 'ObjectLiteral';
}
};
/*
* Pseudoclassical object designed as a non-singleton
*/
var PseudoclassicalInstance = function(){
this.getName = function(){
return 'Pseudoclassical';
};
};
/*
* Functional object designed as a non-singleton
*/
var FunctionalInstance = function(){
return {
getName: function(){
return 'Functional';
}
};
};
Performance Test Runner
I setup JsTestDriver to execute the test cases against Firefox. One advantage of JsTestDriver is I can run my test cases against any browser! Each performance test will instantiate its creational pattern 1.4 million times as indicated by the RUN_TIMES constant. Additionally, I ran the performance test suite twenty times to gather an adequate average.CreationalPatternsPerformanceTest = TestCase("CreationalPatternsPerformanceTest");
var RUN_TIMES = 1400000;
var performanceTest = function(name, f){
console.time(name);
for (var i = 0; i < RUN_TIMES; i++){
f();
}
console.timeEnd(name);
};
CreationalPatternsPerformanceTest.prototype.testResponseTimes = function(){
performanceTest("ObjectLiteral (singleton)", function(){
ObjectLiteralSingleton.getName();
});
performanceTest("Pseudoclassical (singleton)", function(){
PseudoclassicalSingleton.getName();
});
performanceTest("Functional (singleton)", function(){
FunctionalSingleton.getName();
});
performanceTest("Pseudoclassical (instance)", function(){
new PseudoclassicalInstance().getName();
});
performanceTest("Functional (instance)", function(){
FunctionalInstance().getName();
});
};
Object Literal (singleton) | Pseudoclassical (singleton) | Functional (singleton) | Pseudoclassical (instance) | Functional (instance) | |
---|---|---|---|---|---|
Avg (ms) | 1093 | 1095 | 1094 | 3301 | 3341 |
Min (ms) | 1070 | 1064 | 1062 | 3225 | 3246 |
Max (ms) | 1112 | 1114 | 1113 | 3460 | 3476 |
Areas of Interest
I had three specific comparisons I wanted to evaluate heading into this experiment:
- Object literals vs the others (functional and pseudoclassical): I knew object literals were going to be the most efficient. However, I really wanted to see how much faster they really are. The result was surprising. Object literals (1093 ms) barely out performed functional (1094 ms) and pseudoclassical (1095 ms) response times. This difference is really negligible. Unlike object literals, functional and pseudoclassical objects can provide security. And the performance degradation is almost non-existent.
- Functional vs Pseudoclassical: There are always debates over these two objects. While I personally prefer pseudoclassical objects when security is a concern I definitely wanted to see which one was the more performant option. In the end, neither pattern distanced itself from the other in regards to performance.
- Singletons vs Instance based Objects: I knew singletons would be the most efficient. However, I wanted to see what this difference really was. On average, singletons were three times faster.
Lessons Learned
- Prefer singletons over instance based objects when possible.
- Prefer object literals when you are not concerned about privacy.
- The performance metrics alone are not enough to sway the functional vs pseudoclassical debate.