简体   繁体   English

Node.js性能优势

[英]Node.js performance advantage

From what I understand, Node.js has a main thread for the event loop which handles all incoming requests and does IO asynchronously, while traditional multi-threaded web servers handle each incoming request with a separate thread (does everything asynchronously if seen from the main thread's point of view). 据我了解,Node.js有一个用于事件循环的主线程,该线程处理所有传入请求并异步执行IO,而传统的多线程Web服务器使用单独的线程处理每个传入请求(如果从主线程看,则异步执行所有操作)线程的观点)。

In terms of IO, since IO operations are handled asynchronously in both cases, I don't quite see how Node.js can give a performance boost here. 在IO方面,由于IO操作在两种情况下都是异步处理的,所以我不太了解Node.js如何在此处提高性能。

In terms of CPU, isn't Node.js just trading responsiveness for better memory usage? 就CPU而言,Node.js是否只是为了提高内存使用率而交换响应能力? For an extreme example, suppose there's no IO, then Node.js just sums up several threads' work into one. 举一个极端的例子,假设没有IO,那么Node.js只是将多个线程的工作汇总为一个。

I see one major advantage of Node.js is to hide multi-thread programming details behind the framework and simplify programmer's work. 我看到Node.js的一个主要优点是将多线程编程细节隐藏在框架的后面,并简化了程序员的工作。 But could anyone please help explain what the performance advantage is? 但是任何人都可以帮助解释什么是性能优势吗?

Help appreciated. 帮助表示赞赏。

Node.js of course requires that you design your code in such a way that it won't do intensive computations in the main thread. 当然,Node.js要求您以不在主线程中进行大量计算的方式设计代码。 You don't have auto threads for each request but you can explicitly spawn them if it makes sense. 您没有针对每个请求的自动线程,但可以在需要时显式生成它们。

The idea is that typically , responding to a request is very light computationally, and so it is possible for a single thread to handle hundreds of thousands of simultaneous clients, by letting the server handle other requests while any lengthy process required for a request is being done asynchronously. 这个想法是, 通常 ,对请求的响应在计算上非常轻巧,因此单个线程有可能通过让服务器在处理请求所需要的任何漫长过程时处理其他请求来处理成千上万的并发客户端。异步完成。

Node.js (and nginx which uses the same single-threaded design) are faster and use less resources than thread-based servers because they're optimized for those typical requests. 与基于线程的服务器相比,Node.js(和使用相同单线程设计的nginx)速度更快,占用的资源更少,因为它们针对这些典型请求进行了优化。 They remain fast anyway when you do need threads, but the point is, usually you don't, and nginx is an even better example, optimized for serving static resources without any computation at all. 无论如何,当您确实需要线程时,它们仍然保持快速运行,但关键是,通常您不需要,而nginx甚至是一个更好的示例,针对完全不需要任何计算的静态资源进行了优化。

Moreover, Node.js apps use the same JavaScript context to handle every request, which, in some applications, saves a huge amount of overhead when you do perform computations. 此外,Node.js应用程序使用相同的JavaScript上下文来处理每个请求,在某些应用程序中,当您执行计算时可以节省大量开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM