Reputation: 1837
Normally I require files with this way:
async = require('async')
But I want to call like this:
tools.js:
async = require('async')
exports.async = async
other.js
tools.async.parallel([......
Is this causes performance problems?
Upvotes: 0
Views: 29
Reputation: 707318
There should be no performance problems with passing a single instance of the async module around. tools.async.parallel([...])
is slightly slower to run than a locally assigned async.parallel([...])
, but that difference is likely immaterial.
On the other hand, async = require('async')
caches the first loaded module, so there is little issue with just using the require('async')
again.
A common design goal when breaking your code into modules is to make most of your modules be reusable components. That means that they load their own things that they need using their own require()
statements for each other module that they depend upon. Now, that won't necessarily be the case for every module, but it should generally be part of your design thinking to make as much resuable code as possible so you can reuse code in other projects without redesigning it. To do that, you usually want modules to just require()
in any dependent modules on their own as this simplifies maintenance and use.
Upvotes: 1