4

I've been reading a few documentations on Node.js recently to try and get me started on some personnal work. During the process, a thought came up again and again, but I couldn't quite get a satisfactory answer.

In every piece of code I've seen so far, there were quite a few require(). I understand the primary function of it is to include modules. But it seems like people used it to include other Javascript files in their main, too. As far as I know, there is no proper way to do that in Javascript (as stated here: How do I include a JavaScript file in another JavaScript file?).

Here is my concern; seperating files according to their functions is obviously good to improve readibility. However, isn't require() slowing down drastically the programm since it has to fetch information by opening the Javascript file and so on? If so, is the difference significant enough to worry about in a web application?

Wouldn't it be more prudent to write a programm that automates the assembly process in order to obtain a single file (or at least a more monolithic one) before submitting the version?

Community
  • 1
  • 1
Kathandrax
  • 914
  • 14
  • 26
  • 1
    Yes, it's mainly the difference between the development version and the release version. You can keep your code nice and clean while developing, and when you are ready to release, package it and send the minified version to the test/production server when you are done ;) btw, the question you linked to is from 2009, js technologies since then have changed quite a lot, and now with commonjs, webpack, gulp, grunt... you can have a nice clean code base for your javascript projects – Icepickle Jan 22 '16 at 20:11
  • 5
    afaik requried modules are cached by node, so I don't think there can be a major performance downgrade due to multiple `require` calls – Vsevolod Goloviznin Jan 22 '16 at 20:20
  • 1
    @VsevolodGoloviznin Is right, the modules are cached: http://fredkschott.com/post/2014/06/require-and-the-module-system/ – theJoestJoeToEverJoe Jan 22 '16 at 20:20
  • 1
    FWIW the module caching in node is more than just caching the files. All modules are singletons. Therefore they are compiled and executed ONLY ONCE. This is nice because if you need a singleton in node.js you don't need to write anything. The `module.exports` object is a singleton. – slebetman Apr 17 '17 at 03:39

1 Answers1

6

On the server, any performance impact here will be inconsequential relative to everything else the server is doing (like interpreting & executing all this javascript!). Of course, if you are having perf issues and are suspicious of this claim it's never a bad idea to measure.

On the other hand, if we're talking about javascript that will be sent over a network to a browser then it's a different story... there are a ton of tools for bundling & minifying code so browsers can download/execute it faster. That said, HTTP 2 changes this a bit - with this new protocol, many files can often be downloaded in parallel faster than big bundles.

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • 3
    Actually, when node.js encounters a require() line for an uncached module, the file is loaded in the same thread and the rest of the server will wait. If the file is coming from a slow resource (e.g. NFS) then the impact is NOT inconsequential. That's why it's good to structure code in such a way that all requires are called during initialization. – Ted Bigham Jan 22 '16 at 21:37