
How JavaScript works: The building blocks of Web Workers - zlatkov
https://blog.sessionstack.com/how-javascript-works-the-building-blocks-of-web-workers-5-cases-when-you-should-use-them-a547c0757f6a
======
streptomycin
I wish Shared Workers had more browser support (currently just Chrome and
Firefox). Service Workers are great, but not for long-running processes.

With a Shared Worker, you can have one instance of your app that will actually
keep running continually as long as you have a tab open. Then you can treat
multiple open tabs as multiple different clients of the Shared Worker. All
state and communication with the server can be handled by the Shared Worker,
reducing a lot of duplicated effort and cross-tab coordination. It's pretty
great... but only in Chrome and Firefox.

~~~
paws
I realize it's not the same thing, but just FYI, Safari 11.1 is apparently
adding ServiceWorker support. [1]

One year ago it didn't seem very likely this would land. Maybe the future
holds some promise for SharedWorkers on Safari too.

[1] [https://developer.apple.com/safari/technology-
preview/releas...](https://developer.apple.com/safari/technology-
preview/release-notes/)

------
kipple
Another nit to pick: "Copying the message: the message is serialized, copied,
sent over, and then de-serialized at the other end. The page and worker do not
share the same instance, so the end result is that a duplicate is created on
each pass. Most browsers implement this feature by automatically JSON
encoding/decoding the value at either end."

This is untrue, modern workers use the structured clone algorithm[0][1], not
JSON.stringify() & JSON.parse(). This algorithm has many advantages, such as
preserving object references, supporting built-ins like Maps and Sets, and
allowing cyclical/recursive structures.

[0] [https://developer.mozilla.org/en-
US/docs/Web/API/Web_Workers...](https://developer.mozilla.org/en-
US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) [1]
[https://www.w3.org/TR/2012/CR-
html5-20121217/infrastructure....](https://www.w3.org/TR/2012/CR-
html5-20121217/infrastructure.html#safe-passing-of-structured-data)

~~~
zlatkov
Thanks! I'll put this into the article.

~~~
kipple
Nice! Glad to help

------
lioeters
Well-written article - I'll enjoy reading the rest of the series in depth.

One nit to pick: "Web Workers run in an isolated thread in the browser. As a
result, the code that they execute needs to be contained in a _separate file_.
That’s very important to remember."

This isn't totally true. It is possible to create an "object URL" [0] from a
string and run that as a worker, instead of from a separate file:

    
    
      new Worker(URL.createObjectURL(new Blob([
        'self.onmessage = function(msg) { ... }'
      ]))
    

I've seen this technique used in several libraries for WebAudio, to run a more
accurate timer/scheduler.

[0] [https://developer.mozilla.org/en-
US/docs/Web/API/URL/createO...](https://developer.mozilla.org/en-
US/docs/Web/API/URL/createObjectURL)

~~~
zlatkov
Good catch. Will add this example :)

