How JavaScript works: The building blocks of Web Workers
blog.sessionstack.com
blog.sessionstack.com
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.
https://developer.mozilla.org/en-US/docs/Web/API/BroadcastCh...
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...
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... [1] https://www.w3.org/TR/2012/CR-html5-20121217/infrastructure....
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...