admin管理员组文章数量:1415645
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)
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 badges1.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
Reset to default 7The 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) {
...
}
本文标签: javascriptHow to use require inside global and local functionStack Overflow
版权声明:本文标题:javascript - How to use require inside global and local function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745159561a2645378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论