简体   繁体   中英

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

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.

In terms of CPU, isn't Node.js just trading responsiveness for better memory usage? For an extreme example, suppose there's no IO, then Node.js just sums up several threads' work into one.

I see one major advantage of Node.js is to hide multi-thread programming details behind the framework and simplify programmer's work. 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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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