0

Im using node JS aplication and I've created new js file with module and In this module I export just one function,in this module lets say I've additional two functions for internal use only and should not be exposed outside, each function use different require modules like following:

module.exports = function (app, express) {

    var bodyParser = require('body-parser'),
        url = require('url'),
        http = require('http');

.....
};


function prRequest(req, res) {

    httpProxy = require('http-proxy');
....

}

function postRequest(req, res) {

 url = require('url');
....

}

My question is from best practice where should I put the require (for url http etc)

1.inside every function that need it?in my case internal and external

2.globally in the file that every function can use?

3.if two is not OK where should I put the require URL which I should use in two functions?better to put in both function or in global or it doesn't matter

1 Answer 1

6

The modules should be exposed outside the functions as calling require each time the function is called adds extra overhead. Compare:

const url = require('url');
const start = Date.now();

for (let i = 0; i < 10000000; i++) {
    url.parse('http://stockexchange.com');
}

console.log(Date.now() - start);

to:

const start = Date.now();

for (let i = 0; i < 10000000; i++) {
    require('url').parse('http://stackexchange.com');
}

console.log(Date.now() - start);

On my machine, the former takes 95.641 seconds to finish executing, while the latter takes 125.094 seconds. Even if you export the function that uses the required module, it will still have access to other variables within its file when imported. So I would declare the modules locally in each file where they're needed, and not globally.

Edit: this would mean you'd want to do this instead:

var bodyParser = require('body-parser'),
    url = require('url'),
    http = require('http');

module.exports = function (app, express) {
    ....
};

var httpProxy = require('http-proxy');

function prRequest(req, res) {
    ...
}
3
  • Thanks Voted up! so just to verify what you are said. you will keep things like in my post,for each function i use her own require.is it correct?2. what do you do with the url which is needed in more then function,thanks in advance!
    – 07_05_GuyT
    Jul 11, 2015 at 9:09
  • No. I edited my answer to clarify what I meant. If you need url in more than one function, then you'd define it outside of the functions like in my post edit.
    – kpimov
    Jul 11, 2015 at 11:19
  • So just to clarify, requries which needed to be more than one function should be on global,and when require are used in specific file only should be in this function and not globally,please correct me if im wrong
    – 07_05_GuyT
    Jul 11, 2015 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.