Question

I love javascript and Node, but when i declare Objects.. i don't know what is best practice and high performance. Im interest high performance.

"use strict";

module.exports = SignUpBusiness;
SignUpBusiness.__proto__ = new SignUpBusiness;


function SignUpBusiness(){

    this._certificate   = null;
    this._db            = require('mongoose');
    this._model         = require('../models/User.js');
    this._userModel     = this._db.model('User', this._model.UserSchema);
    this._result        = false;

    this.runLoginProcess = function(credentials , next){
         //code
    }

    // etc.

}

Or this way:

"use strict";
module.exports = {

    _certificate   : null,
    _db            : require('mongoose'),
    _model         : require('../models/User.js'),
    _userModel     : this._db.model('User', this._model.UserSchema),
    _result        : false,


    runLoginProcess: function(credentials , next){
          //code
    }
    // etc.
}

Thanks a lot! And.. sorry for my English.

Was it helpful?

Solution

Using prototypes (the first choice) is the best way. Prototypes are best practice and reduce memory consumption (otherwise you'll redeclare the memory on each new object).

According to Louis, the above may be inaccurate:

If the module's export is assigned to foo, then foo.proto._proto_.proto is defined, but foo.proto._proto_.proto._proto_ is not. The second choice has a depth of 1: foo.proto is defined but not any deeper. Not only is the first case taking more memory but it is also slower because field lookups will have to traverse a deeper chain.

I cannot test this for my own answer right now. I will try to later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top