From the Mozilla documentation:
Web Workers is a simple means for web content to run scripts in background threads.
Considering Javascript is single-threaded, are web workers separate threads or processes? Is there shared memory that classifies them as threads?
From the Mozilla documentation:
Web Workers is a simple means for web content to run scripts in background threads.
Considering Javascript is single-threaded, are web workers separate threads or processes? Is there shared memory that classifies them as threads?
Share Improve this question edited Jul 19, 2021 at 3:23 simhumileco 34.7k17 gold badges147 silver badges123 bronze badges asked Mar 1, 2019 at 7:11 NeoNeo 3,7356 gold badges28 silver badges38 bronze badges 2- You could share memory between workers and main thread with SharedArrayBuffer but due to spectre vulnerability it is disabled in most browsers (it can be used as a high-resolution timer) – marzelin Commented Mar 1, 2019 at 8:45
- For some OSes it does not matter. For Linux specifically threads and processes are the same thing. On Linux you can start with a thread, cut off connections to your parent thread and do some other configurations and end with a process. You can also start with a process, join a process group and do some other configurations and end with a thread. Linux implement threads and processes in the same data structure – slebetman Commented Jul 19, 2021 at 3:29
4 Answers
Reset to default 5They run in background threads, but the API pletely abstracts from the implementation, so you may e across a browser that just schedules them to run on the same thread as other events like Node does. Processes are too heavyweight to run background tasks.
Considering Javascript is single-threaded
JavaScript is not single-threaded.
The main part of a JavaScript program runs on an event loop.
Long-running processes (XMLHttpRequest being the classic example) are almost always farmed out to stuff that runs outside the event loop (often on different threads).
Web Workers are just a means to write JavaScript that runs outside the main event loop.
are web workers separate threads or processes? Is there shared memory that classifies them as threads?
That's an implementation detail of the particular JS engine.
As per the MDN:-
The Worker interface spawns real OS-level threads, and mindful programmers may be concerned that concurrency can cause “interesting” effects in your code if you aren't careful.
Reference:- https://developer.mozilla/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#about_thread_safety
The documentation does not define whether the web worker runs in a separate thread or process (or another similar construct). So, depending on the hardware architecture of the processor on which the program is executed, the Operating System and the implementation of the JavaScript engine used, it may be different.
However, I guess that the essence of this question is: Can the Operating System use multiple CPU cores by using web workers? If so, the answer is: YES!!! Even regardless of the implementation of the JavaScript engine!
As long as the processor has many cores, and the Operating System can make use of them, even if the Web Worker's script is executed within another thread of the same process, these threads will be able to run on different cores because the "process" is a construct of an Operating System and itself can run on several processor cores, just as several processes can run on a single core.
P.S. If you want the code to be executed 100% in another process, delegate it to another service (e.g. running on a different server).