最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use require inside global and local function - Stack Overflow

programmeradmin1浏览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

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

Share Improve this question edited Jul 11, 2015 at 16:58 Louis 152k28 gold badges286 silver badges329 bronze badges asked Jul 11, 2015 at 6:16 07_05_GuyT07_05_GuyT 2,88715 gold badges48 silver badges92 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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.');
}

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

to:

const start = Date.now();

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

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) {
    ...
}
发布评论

评论列表(0)

  1. 暂无评论