Here is a quick overview, in case you don't know much about them:
- There is no shared state, so every single message is transparently serialized back and forth. Did your thread processed 100MB of data? Well let me serialize that for you to get it on the other thread. Is there some global structure you want to work on parallel with multiple threads - like an image for example? Forget about it.
- There is no shared code, so worker threads must be created with their source independently - as a constructor argument. Packaging must be done basically separately for your "thread".
Let me translate this for you: Worker threads are a pain to even package in their own files, you can't call something like:
No, that would be too easy, since from that method I could be able to access shared state, so let's make it awkward and weird, why not:
and from the new file call:
All the libraries, that you use both in your worker, or in your main code? Better load them twice.
But wait, there's more!
There is a new object visible named self, that is the worker reference (if it's a script inside a window, self == window).
So let's say you would want to process async a bunch of data, whenever it's being sent from the parent - again, there is no shared context, well why not go with:
Looks simple, isn't it? Except it's in a different bloody file, and did I mentioned that breakpoints in the worker thread code don't stop?