How does a web worker work?
Hello, we say javascript is single threaded, I was wondering how come that it's possible to run multiple web workers which act as different threads? What happens the the hood? Is it that our browser, built say in c++ has many threads available and each one is assigned to a particular thread?
But still, our web page is still single threaded, no? Where do the other threads run?
19 Replies
imagine it as running a 2nd entire js process that communicates back by passing text messages to the main process
that's it
yup noted, thanks !
will try to implement something just to understand how web workers work, will come back later on
they arent as complicated as it might seem
yep will have a look at them
"Web workers" are just a fancy term for "spawn another thread". While JS is inherently single threaded, years ago web devs figured out how to make use of more than one thread on the host machine. According to MDN they've been available for the past decade so…not new tech at all :p
Just note that as soon as you introduce a web worker your page is no longer single threaded, so race conditions etc are a concern.
i wouldnt call it a thread, as you are basically running a 2nd javascript file in a different and separate environment
It's another environment being run on a seperate thread, so I'd call it a thread :p
it is closer to running a whole new browser process than a thread, but i can't say you are wrong because you arent
so, you are technically correct
It literally spawns another thread. Sure, a lot of other stuff comes along with it, but that's more an implementation detail IMO
im not saying that it doesn't, but that feels like an implementation detail and not a part of the spec
in other languages, when you spawn a thread, you just spawn a thread from code in the same executable
or execution context
in c# and python, you just pass a function
in javascript, it feels like you execute a whole new process because the code is in another file
just like in php, which basically runs multiple processes with the code of another file
im not saying you're wrong, but it has feet and a beak like a duck, but is a platypus
Hello, was just playing around with web workers, I have one question

see both are in different scripts but both console.log in the same console, how is that possible?
because the console is a resouce that's available from the web workers
https://developer.mozilla.org/en-US/docs/Web/API/console
Note: This feature is available in Web Workers.that's it there's no other magic to it, or anything like that simply is available and you can use it
ok noted, I was wondering how it's possible because the main thread is the one which normally should use the browser and since the console forms part of the browser, I would believed that the console logs can't be shared
there are ways to share resources between threads, in c++
which is the language used for google chrome
oh
you are right
Thanks !!
you're welcome
btw, sharing the console is an implementation detail
The big limitation you have with workers, which makes it very different from native threads, is that you can't share memory. You have to copy data back and forth using a structured clone, so if you have a big 2GB memory buffer that you want multiple threads to do work on, like filtering a database or doing some kind of analysis on it, you're gonna be in trouble. We faced that limitation on a previous project and i still don't know if there's a way around it that doesn't involve isolating that entire part of your project in webassembly
Shared array buffers can help with that, but then you have to convert to/from a buffer