you have ever used a Web Worker, you know the power of this technology. Same with if you have used a Service Worker. The fact alone that they allow multithreaded programs for a single threaded language is electrifying enough, but oh, no... there's so much more.
If you don't know what a Service Worker or what a Web Worker is, then this is exactly for you. This won't cover in depth coding examples of how these work, but rather explain what they do. Want to know more? Just sit back and read
THE BATTLE OF THE WORKERS!!!
Got your attention now, didn't I?
What are workers?
THE WEB WORKER
A worthy opponent? Oh, yes!
let worker = new Worker('webworker.js');
webworker.js would be the js file that is to be executed as a web worker. This web worker feature is a worthy opponent. Let's meet the second and last opponent of this fantasy...
THE SERVICE WORKER!!!
Don't you hate it when you're offline? When all you can do is play minecraft or something not requiring internet? I wish they had something to cache websites and serve that cached file when you're offline, but there isn't something like that... right???
SERVICE WORKER TO THE RESCUE!
A service worker acts like a proxy between the website and the internet. Technically, it can do heavy computations as well, but it's job is to make sure that you get the right resource when you load a webpage from a website. It basically sits on your computer and loads a file when you request something based on what it's told to do. This is the main purpose of the service worker. It, too also has a seperate thread. It can do everything from serving cached files that are big and take up internet data to... even making a website work offline! OOOOOOOO, now wouldn't that be nice?!
Registering one is as simple as
serviceworker.js is the file that is being treated as a service worker. This isn't a tutorial on how to use them, just showing what they are capable of.
Now that we have shown both opponents, let's see how they are different, then see which of them wins the title for "best worker".
postMessage() feature, while Service Workers do not (there is another way, but let's save that for now). Also, web workers are only accessible by the script that called it (unless you create a shared worker). Service workers, rather have "clients", web pages that it controls and serves content to.
window object or local storage/session storage. They can use indexedDB however.
Either way, they are both different for their different uses.
The winner? A TIE!
I just couldn't choose one...
The thing is, they are both amazing and both really great to use. Depending on what you want to do, you could need a Service worker, a Web worker, or both! It depends on what you are doing.
Let's review shall we?
A Service worker is:
- Can be used offline.
- Is great for caching files and serving them.
A Web worker is:
- Used for heavy computational processes.
- Helpful to minimize main thread work.
So, as you can see they are good for what they are made for.
I hope you liked this tutorial, and I hope you found it helpful. Upvote if you liked it. Please give your comments/questions/feedback in the comments section down below. If you didn't like it, what could I do better?
That's all for now. Bacon Boi out!