Modular vs. Prototype - Nodejs?
I work in Nodejs
. I worked in modular pattern
. It's easy and simple to code.
Note
A colleague of mine said the Prototype pattern is the best approach for Nodejs and the modular pattern is slow.
My example code
module.exports = {
get : funcntion(){
//stuff
},
set : function(){
//stuff
}
}
Question
Which pattern is best for a real-time web application, or generally application context using Nodejs?
source to share
Let me express my thoughts here:
// vinoth.js
var Vinoth = function () {};
Vinoth.prototype.log = function () {
console.log('Hello Vinoth');
};
module.exports = new Vinoth();
// app.js
var vinoth = require('./vinoth.js');
vinoth.log();
Simple Template module
//vinoth.js
module.exports = function () {
console.log('Vinoth');
}
// app.js
var vinoth = require('./vinoth.js');
vinoth();
What am I from understand
:
Prorotype pattern
helps you with inheritance
and extends functionality, and there is only one instance of functions in memory, no matter how many objects. Vinoth.prototype.log
is added to prototype
, and this function is not created again for new objects.
In Modular pattern
for each object, a new instance of the function is created in memory, but helps you with encapsulation
.
source to share