InterviewSolution
Saved Bookmarks
| 1. |
If Node.js is single threaded then how does it handle concurrency? |
|
Answer» The main loop is single-threaded and all async calls are managed by libuv library. For example: const CRYPTO = REQUIRE("crypto");const start = Date.now();function logHashTime() { crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => {console.log("Hash: ", Date.now() - start); });}logHashTime();logHashTime();logHashTime();logHashTime();This gives the output: Hash: 1213Hash: 1225Hash: 1212Hash: 1222This is because libuv sets up a thread pool to HANDLE such concurrency. How MANY threads will be there in the thread pool depends upon the number of cores but you can override this. |
|