web workers in javascript - an introductory guide

3
Web Workers in JavaScript - An Introductory Guide In JavaScript, web workers allow parallel programming to be advantageous for developers. Parallel programming allows programs to perform several computations at the same time. Web Workers is a browser feature that enables scripts to be executed on a separate thread from your web application's main execution thread. This enables your web application's main thread to run without being blocked by sluggish scripts in your application.

Upload: jack.forbes234

Post on 27-Jan-2021

1 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Web Workers in JavaScript - An Introductory Guide

Web Workers in JavaScript - An Introductory Guide

In JavaScript, web workers allow parallel programming to be advantageous for developers. Parallel programming allows programs to perform several computations at the same time. 

Web Workers is a browser feature that enables scripts to be executed on a separate thread from your web application's main execution thread. This enables your web application's main thread to run without being blocked by sluggish scripts in your application. 

Page 2: Web Workers in JavaScript - An Introductory Guide

Isn't JavaScript already asynchronous? 

Well, kind of. This was something that troubled me a lot of times when I first heard about JavaScript. The synchronous, single-threaded language of JavaScript. JavaScript, however, has features that allow you to execute asynchronous code that is managed by browser engines (on your client) or operating systems (on NodeJS) that are capable of running multi-threaded code. 

We work with asynchronous methods in JavaScript by either using callbacks, Promises or async/await. We'll use Promises as an example for exploring asynchronicity in JavaScript. 

Promises are proxies for values that are not yet available when the Promise is created. This lets you organize parts of your code to run when the value becomes available or if something goes wrong. 

This does not mean that your code is running asynchronously. As I mentioned before, JavaScript code is executed on a single thread. The callback that processes the response from your created Promises still runs on your single main thread. Promises do, however, allow you to spawn asynchronous tasks, such as file I/O or making an HTTP request which runs outside of your code. This allows your main thread to work on something else while waiting for these tasks to return a response. This means that the callback functions which run after a response is received are called asynchronously. 

I feel that the distinction between your code running asynchronously vs. being called asynchronously is important, as you will see that you are not able to perform computationally expensive tasks without blocking your main thread, even when using Promises. 

Page 3: Web Workers in JavaScript - An Introductory Guide

You can read more in detail about web workers in JavaScript and how you can use them here: 

https://www.loginradius.com/blog/async/web-workers/