Giter Club home page Giter Club logo

web-workers's Introduction

Web Workers

Table of contents

What is their purpose?

Web Workers allows running code (JS file) in a background thread separate from the main execution thread of an app.

Time-intensive processing can be delegated to them not to block/slow down the main thread.

Concepts and usage

A worker is an object created using a constructor (e.g. Worker()) that runs JS file in the background thread.

Most of the standard JavaScript sets of functions are available from inside the worker. But there are some exceptions: for example, you can't access DOM (more details).

Communication between the worker and the main thread is done via a system of messages. Both sides have a postMessage() method to send their messages.

The onmessage event handler is used to respond to the message (contained within the message event data property). The data is copied (using a structured clone algorithm) rather than shared.

Worker types

  • Dedicated workers - utilized by a single script.
  • Shared workers - utilized by multiple scripts running in different windows, IFrames, etc., within the same domain.
  • Service workers - act as proxy servers between the app, the browser, and the network.

Dedicated worker

A dedicated worker is created using the Worker() constructor, specifying the URL of a script to execute.

E.g.

const worker = new Worker('worker.js');

To start the demo run:

npm run start:worker

Shared worker

A shared worker is created using the SharedWorker() constructor, specifying the URL of a script to execute.

E.g.

const sharedWorker = new SharedWorker('shared-worker.js');

To start the demo run:

npm run shared-worker

Service worker

A service worker is an event-driven worker. It can control the routes it is associated with by intercepting and modifying navigation and resource requests.

It is registered using the ServiceWorkerContainer.register() method.

E.g.

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/service-worker.js');
}

To start the demo run:

npm run service-worker

and then open http://localhost:8888/

web-workers's People

Contributors

viatcheslav-zadorozhniy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.