20

I haven't been able to find any resources that explain the difference between Web Workers (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers), which have been around for years and enable PWA's, and Worker Threads (https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads), which have recently been released in Node.js.

To my understanding both are simply ways of allowing JavaScript to run code in multiple threads. So why are Worker Threads being released as a "new" thing?

yoursweater
  • 1,883
  • 3
  • 19
  • 41

1 Answers1

25

Web Workers are a technology that exists in browsers.

Worker Threads are a technology that exists in node.js.

They have similar goals, but due to environment differences, they have different implementations.

To my understanding both are simply ways of allowing JavaScript to run code in multiple threads.

Yes, that is correct. With significant limitations such as no access to the same variables as the main thread and in the browser, no access to the DOM. Communication between threads and the main thread is generally done via messaging.

So why are Worker Threads being released as a "new" thing?

Node.js has not had the ability to run Javascript code in threads until the "new" Worker Threads. node.js never had Web Workers. Web Workers have existed in the browser for awhile. Before Worker Threads, developers had to use multiple processes to involve additional CPUs or to keep CPU-intensive code from blocking the event loop. Now, node.js developers can do this with Worker Threads.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Nodejs did have spawnchild/cluster.. but difference being that cluster runs on new core of cpu, whereas threads run on same core. you can multiple threads on a single cpu but ideally 1 thread per core. – Muhammad Umer May 29 '21 at 14:50
  • @MuhammadUmer - A modern operating system uses multiple cores for threads too. – jfriend00 May 29 '21 at 16:00
  • Isn't a thread associated with a single core, so a thread would always remain with a core it was started in. And a single core could have N threads. And X cores could have N*X threads. Last point was that fewer threads there are per core, the better it's due to cost of "Context Switching" – Muhammad Umer May 29 '21 at 22:57
  • 1
    @MuhammadUmer - When a thread is actually executing, it won't jump from one core to another, but if the OS time slices it out to let other threads have their fair turn, it can come back on any core unless it is bound to a specific core. Plus threads are not bound to the core that their parent process is running on. Keep in mind that in a modern OS, there can already lots of other threads that are part of the housekeeping of running the OS. When I look in Process Explorer on my Windows 10 system right now, there are already hundreds of threads. – jfriend00 May 29 '21 at 23:18
  • It would be good to clarify why they didn't just implement Web Workers in Node.js, rather than creating a new API, i.e. how the APIs differ. – Ciro Santilli OurBigBook.com Nov 05 '21 at 16:00