简体   繁体   English

在node.js应用程序的每个模块中要求使用某些相同软件包的影响和替代方案?

[英]Impact and alternatives on requiring some of the same packages in every modules of my node.js application?

Let's say I have an application as following: 假设我有一个如下应用程序:

  • server.js (main) is requiring different external node packages, like underscore.js . server.js (主要)需要不同的外部节点程序包,例如underscore.js

    var Underscore = require("underscore");

  • server.js is also requiring some modules defined in my application it-self; server.js还需要自己在应用程序中定义的一些模块。 For example it could requires a Router module, to handle Express routes. 例如,它可能需要路由器模块来处理Express路由。

    var Router = require("./sources/router.js");

  • I have then my router.js file as following : 然后我有如下的router.js文件:

     var Router; Router = (function() { function Router(app, oauth) { app.get('/', function(request, response) { // ... }); } return Router; })(); module.exports = Router; 

Questions: 问题:

  • If I want to use underscore inside my Router module, should I re-require again ? 如果要在Router模块中使用underscore ,是否应该再次要求? Should I do that for every modules ? 我应该为每个模块都这样做吗? What is the impact? 有什么影响? I would end up with something like: 我最终会得到类似:

     var Router; Router = (function() { Router.prototype.underscore = require("underscore"); function Router(app, oauth) { app.get('/', function(request, response) { // this.underscore. ... // using underscore ... }); } return Router; })(); module.exports = Router; 

    and

     var Underscore = require("underscore"); var Router = require("./sources/router.js"); router = new Router(); 
  • I could obviously also inject as a parameter it when initializing Router , but this doesn't look to me like a viable option in an application where I may end up using dozens of packages, especially for very general purpose package like this one. 显然,在初始化Router ,我也可以注入它作为参数,但是在我看来,这在应用程序中可能并不可行,因为我可能最终会使用数十个软件包,尤其是对于像这样的通用软件包。

     var underscore = require("underscore"); var Router = require("./sources/router.js"); var router = new Router(underscore); 
  • Alternatively I could set the underscore var as a global one, but I don't really like this option. 另外,我可以将underscore var设置为全局underscore ,但是我真的不喜欢此选项。

  • Is there any other options ? 还有其他选择吗?

  • What is the impact of systematically importing packages in every modules - in term of execution time, memory ? 在执行时间,内存方面,系统地在每个模块中导入软件包有什么影响? I would like to understand the behavior of the node engine in such cases. 我想了解这种情况下节点引擎的行为。

Yes, you should just require it again. 是的,您应该再次require它。 Node caches required modules, so the second time you require something it doesn't actually run that file; 节点缓存了所需的模块,因此第二次您require东西实际上并没有运行该文件。 it just returns the cached object. 它只是返回缓存的对象。 So the memory impact is basically 0 (an extra pointer to the same object, more or less) and the execution time is similarly negligible (the cost of a lookup in an object by the module name). 因此,内存影响基本上为0(或多或少指向同一对象的额外指针),执行时间类似地可以忽略不计(通过模块名称在对象中查找的开销)。

This means that the objects returned by the two require s aren't just identical; 这意味着两个require返回的对象并不完全相同; they're literally the same object. 他们实际上是同一个对象。 Any change to one will affect the other. 对一个的任何更改都会影响另一个。 Because of this, you can extend the module in one place and get those extensions everywhere. 因此,您可以将模块扩展到一个位置,然后将这些扩展扩展到任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM