Reputation: 1151
What is the best way to use NodeJS's require
function? By this, I'm referring to the placement of the require
statement. Isf it better to load all dependencies from the beginning of the script, as you need them, or does it not make a notable difference whatsoever?
This article has a lot useful information regarding how require
works, though I still can't come to a definitive conclusion as to which method would be most efficient.
Upvotes: 1
Views: 2746
Reputation: 13111
In addition to the other answers you should be aware that...
making sure that you call all require()
at the start of your process gives you instant feedback wether all modules can be parsed and initialized. Your process would "fail early" and not depending on it's usage, which is good
OTOH if your program is not a server-like long-running process (and rather a complex command line utility), it could be inefficient to load many modules that are not being used at all.
Basically you should be aware about the require()
effects and decide the best strategy based on the kind of program you are developing. That said, it's usually the best to load "all" modules at startup.
Upvotes: 0
Reputation: 707198
Assuming you're using node.js for some sort of server environment, several things are generally true about that server environment:
So, given that require()
uses synchronous I/O when the module has not yet been cached, that means you really don't generally want to be doing require()
operations inside a request handler. And, you want fast response times for your request handlers so you don't want require()
calls inside your handler anyway.
All of these leads to a general rule of thumb that you load necessary modules at startup time into a module level variable that you can reuse from one request to the next and you don't load modules inside your request handlers.
In addition to all of this, if you put all your require()
statements in a block near the top of your module, it makes your module a lot more self-documenting about what other modules it depends on and how it initializes those modules. If require()
statements are sprinkled all over the code, then it makes it a lot harder for a developer to see what this module is using without a lot more study of the code.
Upvotes: 5
Reputation: 887275
It depends what performance characteristics you're looking for.
require()
is not cheap; it has to read the JS file from disk, parse it, and execute any top-level code (and do all of that recursively for all files require()
d by that file).
If you put all of your require()
s on top, your code may take more time to start, but it won't suddenly slow down later. (note that moving the require()
further down in the synchronous top-level code will make no difference beyond order of execution).
If you only require()
other modules when first used asynchronously, your first request may be noticeably slower, as Node parses all of your dependencies. This also means that any errors from dependencies won't be caught until later. (note that all require()
calls are cached, so the second request won't do any more work)
The other disadvantage to scattering require()
calls throughout your code is that it makes it less readable; it's very nice to easily see exactly what each file depends on up top.
Upvotes: 2