The Node.js Event Loop: Not So Single Threaded
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about Node.js threading, the thread pool and the event loop, as the deeper reference for how async work actually runs.
Recommended by 1 people across 1 different months, 2020 to 2021.
What it is
A conference talk from Node.js Interactive by Bryan Hughes of Microsoft, a member of the Node.js Technical Steering Committee. It explains how the C++ event loop underneath Node.js uses multiple threads even though the JavaScript runs on one, and how that affects performance.
What people said
Recommends Jingshao Chen · 2021-06 · Stack Overflow · on “Javascript efficiently get row number of a value in two dimensional array”
With nodes, you can divide the array into multiple sub-arrays and use async to run the search in parallel: The following script uses two threads and will give you half of the time. async function aGetRow(d, v) { let half = Math.floor(d.length / 2); let remain = d.length - half let r = await Promise.all([ getRow(d.slice(0, half), v), getRow(d.slice(half, remain), v), ]); if (r[0] != -1) { return r[0]; } if (r[1] != -1) { return r[1]; } return -1; } One must note that NodeJS has 4 threads in the thread pool by default. If you split the array more than the threads, you will not gain performance enhancement. Edit: For people interested in the details of NodeJS threading and event loop, I highly
Cites Michael · 2020-09 · Stack Overflow · on “Is parallelization of network requests a good use for Node.js workers?”
Basically, http request in Node.js isn't a blocking operation (it's mostly true). so using a worker here is redundant and not recommended. what would be a better approach This can change from case to case, but this should suffice for most cases: await Promise.all([fetch(...), fetch(...),...]); more information can be found here [this video] .
Cites Sam Claus · 2021-06 · Stack Overflow · on “Using javascript `crypto.subtle` in synchronous function”
TL;DR No, it is not possible to wrap an asynchronous function in a synchronous one in JavaScript and propagate the results. Please see this excellent blog post on sync vs. async functions in various languages [LINK] . The takeaway is that JavaScript is one (of many languages) in which async functions are infectious due to the nature of how the language runs. Async built-in functions are a savior in JS JavaScript runs on one thread. More concretely, all JavaScript related to a particular webpage runs on the same thread to keep the guarantee that only one line of JS will be running in any particular moment. This spares us neanderthal web programmers the responsibility of writing synchronizatio
Cites NeNaD · 2021-12 · Stack Overflow · on “What is the difference between the event loop in JavaScript and async non-blocking I/O in Node.js?”
There are 2 different Event Loops: Browser Event Loop NodeJS Event Loop Browser Event Loop The Event Loop is a process that runs continually, executing any task queued. It has multiple task sources which guarantees execution order within that source, but the Browser gets to pick which source to take a task from on each turn of the loop. This allows Browser to give preference to performance sensitive tasks such as user-input. There are a few different steps that Browser Event Loop checks continuously: Task Queue - There can be multiple task queues. Browser can execute queues in any order they like. Tasks in the same queue must be executed in the order they arrived, first in - first out. Tasks