0

Does calling a function take the same resources and time, as creating an instance of the constructor function in javascript node v8.5.0?

I find that they give me the same result, as:

var repo = function () {

var db = {};

var get = function (id) {
    console.log('Getting task ' + id);
    return {
        name: 'new task from db'
    }
 }

var save = function (task) {
    console.log('Saving ' + task.name + ' to the db');
 }

console.log('newing up task repo');
return {
    get: get,
    save: save
 }

}
 module.exports = repo();

When I replace module.exports = repo(); with module.exports = new repo; it gives me the same result, but I need to know which one is better performance wise.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Abdel-Raouf
  • 700
  • 1
  • 8
  • 20
  • 1
    IMHO, it doesn't matter because you will only have a small amount of repo objects; ideally one. You will only make `repo()` or `new repo` once. – lilezek Nov 23 '17 at 12:19
  • 2
    The difference is so minimal as not to be worth worrying about. You should rather decide how exactly you want your function to be used. `new` or no `new`? – deceze Nov 23 '17 at 12:20
  • if so, what is the best practice you use @deceze ? – Abdel-Raouf Nov 23 '17 at 12:22
  • 1
    I would write a `class` instead of this kind of module constructor function, which would then be used with `new`. But I would probably not export a singleton with no chance of instantiating another instance; there's simply no use for enforcing singletoness to that degree. – deceze Nov 23 '17 at 12:27

2 Answers2

0

by using module.exports = repo(); you are calling repo function and assigning the return value into module.export, here in this will point to the same memory instance

hence by using module.exports = new repo; first you are creating new instance and assigning the new instance to the module.export in this method this will be different memory instance.

so the simple terms it's all depends upon the use-case how are are going to use your code by pointing the same instance or by creating the new instance.

I hope you understand the architecture.

Sultan Khan
  • 308
  • 2
  • 11
0

Since your function does return an object already, it is not a constructor (that initialises an instance through the this keyword) in the traditional sense and should not be called with new. It doesn't even matter whether that is faster or slower - just don't do it. (Hint: new would probably be slower, as it has to create a new instance from repo.prototype and then throw it away).

And of course there's no good reason to use a function at all in this case. You're only calling it once anyway, so you could inline the code:

const db = {};
function get(id) {
    console.log('Getting task ' + id);
    return {
        name: 'new task from db'
    }
}
function save(task) {
    console.log('Saving ' + task.name + ' to the db');
}
console.log('newing up task repo');
module.exports = {
    get,
    save
};

or even simplify to

const db = {};
exports.get = function (id) {
    console.log('Getting task ' + id);
    return {
        name: 'new task from db'
    };
};
exports.save = function (task) {
    console.log('Saving ' + task.name + ' to the db');
};
console.log('newing up task repo');
Bergi
  • 630,263
  • 148
  • 957
  • 1,375