Until now, I used the Revealing Module Pattern to structure my Javascript, like so:
var module = (function() {
var privateVar;
// @public
function publicFunction( ) {
}
return {
publicFunction: publicFunction
}
})();
Although this code works as expected, I read an article lately that this pattern uses a large amount of memory if you have multiple instances, and that it has some speed issues compared to other patterns. Since I enjoy working with this pattern, I did a search for similar patterns without all those "issues" and I came across the Revealing Prototype Pattern. As far as I know, JavaScript`s Prototype has a much better memory management.
Now I'm wondering if it's faster / better for memory to use the Revealing Prototype Pattern? This benchmark surprised me, because the Module Pattern seems to be much faster. Is there any reason?
Also, I couldn't figure out how to have multiple instances with the Revealing Prototype Pattern (compare to the Revealing Module Pattern Fiddle above):
var prototypeModule = function( el ) {
this.init( );
};
prototypeModule.prototype = function () {
var privateVar;
// @public
function init( ) {
}
return {
init: init
}
}();
What am I doing wrong?