Question is simple, even if we use async mode with Bcrypt module in node.js, wouldnt it still use up the cpu for the bcrypt cycles? Its not like a db read which is carried out elsewhere but a CPU operation. So why does the async method has a performance advantage
-
Your question was already answered in this post: [Does asynchronous programming in node.js speed up CPU-bound tasks?](http://stackoverflow.com/questions/17204890/does-asynchronous-programming-in-node-js-speed-up-cpu-bound-tasks) – dee.ronin Nov 25 '15 at 00:37
-
@dee.ronin - That's a generic answer, but not one that explicitly addresses how the bcrypt module is implemented. – jfriend00 Nov 25 '15 at 00:42
1 Answers
The async operations in bcrypt appear to use a native code worker thread which will not block the main node.js thread. See here in the C++ source for an example.
It is true that CPU time is CPU time so it will take the same CPU time whether done synchronously or asynchronously, but because the async version is done on a separate thread, that gives the computer the ability to use multiple cores to run concurrently or to time slice it with the main node.js thread so that main node.js thread is not blocked while doing the crypto operation.
This will give the node.js thread access to more CPU time than it would if the crypto operation was done synchronously (because the crypto CPU time is taken off the main node.js thread), thus a performance advantage for the node.js thread as long as there are at least two CPU cores available.

- 683,504
- 96
- 985
- 979
-
hey thanks. How long would i need to program to be able to answer questions like that :) – noob7 Nov 25 '15 at 20:08
-
I just wanted to stress the importance of at least two cores. I can confirm that on one core and under stress testing, your server will lock up using bcrypts async methods. The solution is probably either a minimum of two cores or offloading the work to an external location, using something like Azure functions or AWS lambda. – Sam Aug 08 '16 at 03:24