-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Article « The Basics of Web Workers » (HTML5Rocks) #2
Comments
« One thing that's remained a hindrance for JavaScript is actually the language itself. JavaScript is a single-threaded environment, meaning multiple scripts cannot run at the same time. As an example, imagine a site that needs to handle UI events, query and process large amounts of API data, and manipulate the DOM. Pretty common, right? Unfortunately all of that can't be simultaneous due to limitations in browsers' JavaScript runtime. Script execution happens within a single thread. » (source: H5R › Basics of Web Workers) |
Getting Started — Creating a WorkerSee also Experiment 01 · Instantiation of a Web Worker (#3). « A Web Worker is created with the Worker() constructor, that takes the URL of a script; the script must obey the same-origin policy. » (excerpts from MDN › Web Worker API). « If the specified file exists, the browser will spawn a new worker thread, which is downloaded asynchronously. The worker will not begin until the file has completely downloaded and executed. If the path to your worker returns an 404, the worker will fail silently. » Classic scriptsSee Experiment 01 · Instantiation of a Web Worker · Classic script Workers usually run classic scripts. Example of a « Background number-crunching worker » (source): Main page
Worker
Module scriptsSee Experiment 01 · Instantiation of a Web Worker · Module script « Workers can instead be instantiated using module scripts, which have the usual benefits:
Note that unlike classic workers, module workers can be instantiated using a cross-origin script, as long as that script is exposed using the CORS protocol. Additionally, the Example of a worker doing « off-main-thread image manipulation » (source): Main page
Worker
Note: Module scripts to instantiate a worker requires to enable experimental web platform features inin Chrome 71 currently. Running the above examples displays following error message on console:
To start Chrome with the
Inline / embedded scriptSee Experiment 01 · Instantiation of a Web Worker · Embedded script To create your worker script on the fly, or create a self-contained page without having to create separate worker files, with Main page
Getting Started — Creating a SubworkerSee also:
Note:
« Workers have the ability to spawn child workers. This is great for further breaking up large tasks at runtime. Subworkers come with a few caveats: they must be hosted within the same origin as the parent page; URIs within subworkers are resolved relative to their parent worker's location (as opposed to the main page). Keep in mind most browsers spawn separate processes for each worker. Before you go spawning a worker farm, be cautious about hogging too many of the user's system resources. One reason for this is that messages passed between main pages and workers are copied, not shared. » (source: H5R › Basics of Web Workers) Getting Started — Creating a Shared WorkerI did not cover the subject much here, as Shared Workers are not well supported yet by mobile browsers. See following examples of creation and use of Shared Workers: |
Communicating with a WorkerVia Message Passing« A Worker can't access the DOM directly. Instead, a Service Worker can communicate with the pages it controls by responding to messages sent via the « A Worker has two methods:
And it has three event handlers:
The Structured Cloning algorithm« The structured clone algorithm is an algorithm defined by the HTML5 specification for copying complex JavaScript objects. It is used internally when transferring data to and from The structured clone algorithm builds up a clone, by recursing through the input object, while maintaining a map of previously visited references, in order to avoid infinitely traversing cycles. Things that don't work with structured clone:
(source: MDN › The structured clone algorithm) Transferable objectsTODO: see H5R › The Basics of Web Workers › Transferable objects MDN › Web APIs › Transferable Objects A marker interface, that represents an object that can be transfered between different execution contexts, like the main thread and Web workers. The ArrayBuffer, MessagePort and ImageBitmap types implement this marker interface. |
Worker environment — An isolated execution contextWorker global context« Workers run in another global context, that is different from the current window and which is represented by either:
Worker scopeIn the context of a worker, both The standalone « By using The Features available to Workers« Due to their multi-threaded behavior, Web workers only has access to a subset of JavaScript's features:
Workers do NOT have access to:
« However you can use a large number of items available under See Functions and classes available to workers for more details. » (source: MDN › Workers API) Loading External ScriptsTo loads
|
Error handling
« The Sample error handler, in main page
Note the |
SecurityRestrictions with Local Access
Same Origin Considerations
|
Reading notes of the primer article of Eric Bidelman (26.07.2010):
HTML5Rocks › The Basics of Web Workers
https://www.html5rocks.com/en/tutorials/workers/basics/
plus some other sources:
WebFundamentals › Service Workers: an Introduction
https://developers.google.com/web/fundamentals/primers/service-workers/
MDN › Web Worker API
https://developer.mozilla.org/en-US/docs/Web/API/Worker/
HTML Living Standard › Web Workers.
https://html.spec.whatwg.org/multipage/workers.html
Merge with links from main READ:
Typed arrays I/O and Structured clone algorithm; no more base64 encoding the data!
Actions
Transferable
objectsTODO
TODO
The text was updated successfully, but these errors were encountered: