terpak
terpak

Reputation: 1151

NodeJS: Most efficient way to use "require"

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

Answers (3)

Udo G
Udo G

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

jfriend00
jfriend00

Reputation: 707198

Assuming you're using node.js for some sort of server environment, several things are generally true about that server environment:

  1. You want fast response time to any given request.
  2. The code that runs for processing requests should not use synchronous I/O operations because that seriously lessens the scalability of the server.
  3. Server startup time is generally not something you need to optimize for (within reason) so if you're going to pay an initialization cost somewhere, it is usually better paid once at server startup time.

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

SLaks
SLaks

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

Related Questions