admin管理员组文章数量:1278819
How does Node.js do synchronous 'require()' calls so quickly during runtime? With Java, we can build binary files before execution using a build tool like Maven. But with Node.js we don't really have build tools, yet the CommonJS synchronous require() statements embedded in our Node.js code surely must be some of the slowest lines of code in our Node.js programs but how do they run so quickly considering that they are loading files at runtime?
Here is some info about CommonJS modules: /
How does Node.js do synchronous 'require()' calls so quickly during runtime? With Java, we can build binary files before execution using a build tool like Maven. But with Node.js we don't really have build tools, yet the CommonJS synchronous require() statements embedded in our Node.js code surely must be some of the slowest lines of code in our Node.js programs but how do they run so quickly considering that they are loading files at runtime?
Here is some info about CommonJS modules: http://fredkschott./post/2014/06/require-and-the-module-system/
Share Improve this question edited May 6, 2015 at 21:57 Alexander Mills asked May 6, 2015 at 21:48 Alexander MillsAlexander Mills 100k166 gold badges534 silver badges910 bronze badges 7- For starters, modules are cached nodejs/api/modules.html#modules_caching – Andrew Lavers Commented May 6, 2015 at 21:53
- thanks Andrew, you're a genius, but what about the first incidence of requiring a module? From your link "Modules are cached after the first time they are loaded. " Yeah so about that first time. – Alexander Mills Commented May 6, 2015 at 21:54
- 6 Well, you know the V8 engine is pretty fast right? That's 8 cylinders worth of code executors, and at 0.5L each you get 4 whole litres of codespeed. That's pretty damn fast. – Andrew Lavers Commented May 6, 2015 at 21:58
- 1 @AlexMills: Why do you think that they are quick? Have you timed them? What did you expect? What do you think does slow them down exactly? (Down from where?) – Bergi Commented May 6, 2015 at 22:13
- 1 @AlexMills: Yes, IO is paratively slow. But it does usually only happen on startup (not in the midst of program execution) - and yes, big programs take longer to load. – Bergi Commented May 6, 2015 at 22:17
1 Answer
Reset to default 14Here are several reasons why the performance of require()
is generally not an issue in node.js programs:
Files are fetched from a local hard drive (relatively fast), not over the internet. And, the local OS has a hard drive cache too so the files may not even be ing from the hard drive, but rather from a memory cache.
Most modules are loaded at server startup when performance is generally not an issue. It is rare to load a new module during a particular server request which is generally where the performance actually matters.
Modules are cached so once any module has been loaded once, the loaded, parsed and run module is already cached and is just immediately returned from the cache.
V8 is relatively quick at what it does.
That said, if you did have lots of brand new require()
statements running during a given http request operation (new statements such that the modules were not cached), then it would seriously slow down the processing of that request and the momentary scalability of your server. But, since you can't really repeat that operation over and over (because once you did it once the modules are now cached), it generally doesn't cause a problem.
But, for good server response times, you probably don't want to be do require()
for the first time in a server request. It would generally be better to just load it at server initialization time so it gets cached or preloaded and you don't have to use your one node.js thread to do a synchronous require()
during a server request.
本文标签: javascriptHow does Nodejs do synchronous 39require()39 calls so quickly during runtimeStack Overflow
版权声明:本文标题:javascript - How does Node.js do synchronous 'require()' calls so quickly during runtime? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741276514a2369760.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论