Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Web Workers and Worker Threads?

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?

like image 846
yoursweater Avatar asked Feb 17 '20 18:02

yoursweater


People also ask

Are web workers threads?

Web Workers are a simple means of running scripts in background threads for web content. Without interfering with the user interface, the worker thread may perform tasks.

What is difference between web worker and service worker?

What is a Service Worker? Unlike Web Workers, Service Workers have a dedicated role of being a proxy between the network and the browser and/or cache. Similar to Web Workers, they are registered in the primary JavaScript file as a dedicated worker.

What are web workers?

A web worker is a piece of browser functionality. It is the real OS threads that can be spawned in the background of your current page so that it can perform complex and resource-intensive tasks. Imagine that you have some large data to fetch from the server, or some complex rendering needs to be done on the UI.

What does worker thread mean?

Worker thread is a continuous parallel thread that runs and accepts messages until the time it is explicitly closed or terminated. Messages to a worker thread can be sent from the parent thread or its child worker threads. Through out this document, parent thread is referred as thread where a worker thread is spawned.


1 Answers

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.

like image 151
jfriend00 Avatar answered Oct 05 '22 22:10

jfriend00