如何要求模块影响Node.js的性能? [英] How does requiring a module affect performance in Node.js?

查看:90
本文介绍了如何要求模块影响Node.js的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在阅读关于Node.js的一些文档,试图让我开始做一些个人工作。在这个过程中,一次又一次地想到了一个想法,但我无法得到满意的答案。

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.

在我见过的每一段代码中,有相当多的require()。我理解它的主要功能是包含模块。但似乎人们用它来包含其他Javascript文件。
据我所知,Javascript中没有正确的方法(如下所述:如何在另一个JavaScript文件中包含JavaScript文件?

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?).

这是我关心的问题;根据功能分离文件显然有利于提高可读性。但是,不需要()大幅减慢程序,因为它必须通过打开Javascript文件来获取信息等等?如果是这样,在Web应用程序中是否有足够的差异值得担心?

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?

推荐答案

在服务器上,任何性能影响相对于服务器正在做的其他事情(如解释和执行所有这些javascript!),这将是无关紧要的。当然,如果你遇到性能问题并且对这种说法持怀疑态度,那么测量它绝不是一个坏主意。

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.

另一方面,如果我们谈论的是javascript那将通过网络发送到浏览器然后它是一个不同的故事...有大量的工具用于捆绑和缩小代码,以便浏览器可以更快地下载/执行它。也就是说,HTTP 2改变了一点 - 使用这个新协议,许多文件通常可以比大包更快地并行下载。

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.

这篇关于如何要求模块影响Node.js的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆