A Simple Introduction to Web Workers in JavaScript
Wasiu Idowu

Wasiu Idowu

Oct 9, 2022

A Simple Introduction to Web Workers in JavaScript

Way back when JavaScript was first created, no one worried too much about performance. JavaScript was designed to be a straightforward language for running small bits of script in a web page. It was a frill — a simplified scripting language for amateur programmers. It certainly
wasn’t meant to run anyone’s business.

Fast forward nearly 25 years, and JavaScript has taken over the web. It has professional responsibilities and — finally — professional features. One example is web workers .

Web workers are designed to let you run big jobs without freezing up the page. For example, imagine you want to do some complex calculations when someone clicks a button. If you start doing the job right away, you’ll tie everything up. The person using the page won’t be able to scroll or click anything. They might even get the dreaded “this page is unresponsive” error message

Web workers are designed to let you run big jobs without freezing up the page. For example, imagine you want to do some complex calculations when someone clicks a button. If you start doing the job right away, you’ll tie everything up. The person using the page won’t be able to scroll or click anything. They might even get the dreaded “this page is unresponsive” error message.

In this situation, you need a way to do work quietly in the background, while the person using your page carriers on in the foreground, undisturbed. Technically, we say the background work takes place on another thread .

(Understanding how threads work is a bit more than we’ll talk about today. But the basic idea is that modern operating systems run hundreds of threads at once, and spend most of their time quickly switching from one thread to another. In fact, they switch so quickly and seamlessly that it seems like everything is happening at once.)

Web workers let you do any time-consuming job in the background. The process is simple:

  1. You create a web worker.
  2. You tell the web worker what to do. (For example, crunch these numbers).
  3. You start the web worker.
  4. When the worker is done, it tells you, and your code goes from there. (For example, show the results on your page).

Let’s dive in!

Wasiu Idowu

Wasiu Idowu

Wasiu breaks down complex topics into smaller, simpler bits

Leave a Reply

4 Comments

Job-Search on Oct 31, 2022

Lorem makes sense at all times

Marijuana Trump on Oct 30, 2022

Lorem ipsum dolor sit amet consectetur, adipisicing elit. Harum voluptates odit iste, id est aliquid molestias consequuntur nemo obcaecati aspernatur non alias doloribus laborum eveniet tempora excepturi quia in voluptatum dolor corporis adipisci odio! Architecto recusandae dolorum ullam nihil obcaecati.

Julian Ward on Oct 26, 2022

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam ex commodi voluptatibus laboriosam est? Soluta, odio natus vel blanditiis mollitia impedit expedita temporibus sit cum dolores enim eveniet eaque dolorum repellat labore hic magnam non assumenda laboriosam.

Machiavelli on Oct 26, 2022

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam ex commodi voluptatibus laboriosam est? Soluta, odio natus vel blanditiis mollitia impedit expedita temporibus sit cum dolores enim eveniet eaque dolorum repellat labore hic magnam non assumenda laboriosam.

Related Posts

Categories