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?
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?
Share Improve this question edited May 23, 2017 at 10:31 CommunityBot 11 silver badge asked Jan 22, 2016 at 20:05 KathandraxKathandrax 1,05417 silver badges27 bronze badges 4- 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 monjs, webpack, gulp, grunt... you can have a nice clean code base for your javascript projects – Icepickle Commented Jan 22, 2016 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 Commented Jan 22, 2016 at 20:20 - 1 @VsevolodGoloviznin Is right, the modules are cached: fredkschott./post/2014/06/require-and-the-module-system – theJoestJoeToEverJoe Commented Jan 22, 2016 at 20:20
-
1
FWIW the module caching in node is more than just caching the files. All modules are singletons. Therefore they are piled 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 Commented Apr 17, 2017 at 3:39
1 Answer
Reset to default 6On 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745254703a4618884.html
评论列表(0条)