最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does asynchronous programming in node.js speed up CPU-bound tasks? - Stack Overflow

programmeradmin1浏览0评论

Earlier today I responded to a question with this answer. In the example that I posted, I used the synchronous version of a call in the bcrypt node module. I chose to use the synchronous version of the call primarily because I thought it made the response look a little cleaner, but I also didn't think that it would affect performance because bcrypt is cpu and memory intensive instead of I/O bound. It was my understanding that node ran almost all your code on a single thread like browsers do, and only used background threads for things like I/O and database access. This lead me to believe that cpu-intensive tasks would still essentially "block" the server, since there were no other threads to offload the work to.

A ment on my response indicated that my assumption was wrong, and after some research I realized that I did't really have a great grasp on how node.js handles this sort of thing. Does asynchronous programming in node.js speed up cpu and memory intensive calls? If so, how does it do it?

Earlier today I responded to a question with this answer. In the example that I posted, I used the synchronous version of a call in the bcrypt node module. I chose to use the synchronous version of the call primarily because I thought it made the response look a little cleaner, but I also didn't think that it would affect performance because bcrypt is cpu and memory intensive instead of I/O bound. It was my understanding that node ran almost all your code on a single thread like browsers do, and only used background threads for things like I/O and database access. This lead me to believe that cpu-intensive tasks would still essentially "block" the server, since there were no other threads to offload the work to.

A ment on my response indicated that my assumption was wrong, and after some research I realized that I did't really have a great grasp on how node.js handles this sort of thing. Does asynchronous programming in node.js speed up cpu and memory intensive calls? If so, how does it do it?

Share edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Jun 20, 2013 at 3:42 TwentyMilesTwentyMiles 4,0893 gold badges31 silver badges37 bronze badges 1
  • "Does asynchronous programming in node.js speed up cpu and memory intensive calls?" --- it pretends it does. Like sand clocks speed up the OS when you're looking at them. – zerkms Commented Jun 20, 2013 at 4:04
Add a ment  | 

1 Answer 1

Reset to default 11

It depends on how the module is implemented.

If the module is implemented without any support for threading then yes, CPU bound processing cannot be done asynchronously. Some functions provide callbacks and my look asynchronous but they're really not. They actually run synchronously and blocks the event loop. Examples of this in javascript is Array.forEach().

But, modules may be implemented to do the processing in background threads. In which case it truly is asynchronous and can speed up CPU bound tasks. At the very least it frees up the event loop to handle ining requests while the background thread is busy puting results.

An example of this is the crypto.pbkdf2() function in node's own Crypto module.

But, how can modules execute code in other threads when node.js runs in a single thread?

The original way this was implemented was simply that the module wasn't written in javascript but was instead written in C/C++ and interfaced to node.js via it's addons API.

But these days even pure javascript modules and functions can spawn threads and/or processes. Node has an experimental module called Cluster that sets up a master/slave cluster of node processes. Your module or code can then run the CPU bound task in a worker process freeing up the main node process to handle the event loop. There are also several threading modules available on npm. Just search for "thread" on npmjs.

So, yes CPU bound tasks can be made to run faster or at least not block the main event loop by running asynchronously.

发布评论

评论列表(0)

  1. 暂无评论