Web Workers
Web Workers
#
website
#
performance
#
react
#
javascript
Have you ever faced a situation in which you are executing a block of code and it
is freezing the UI for a few seconds? If yes then you surely need a solution to
handle that block of code in the background.
A web worker is a JavaScript feature that allows you to run scripts in the
background, separate from the main thread of your web page. This background
execution enables you to perform tasks concurrently without blocking the user
interface (UI) or causing it to become unresponsive. Web workers are particularly
useful for handling computationally intensive or time-consuming operations without
impacting the user's experience.
Complex Calculations: Web workers are ideal for performing complex mathematical
calculations or data processing tasks without impacting the main thread.
Large Data Handling: When dealing with large datasets or files, web workers can
help process and parse the data in the background.
Background Services: Web workers can be used to run background services such as
periodic data synchronization or background notifications.
So, let's try to implement a web worker in a React application.
First, we create a react project, and then we use a service faker to create 25000
user records.
import { faker } from '@faker-js/faker';
In the "app.worker.js" file we will export an arrow function in which we will add
an event listener on the window object(self) to listen to the "message". This
"message" will be triggered from the UI with "users" and "type"(ascending or
descending" variables.
export default () => {
self.addEventListener('message', e => { // eslint-disable-line no-restricted-
globals
if (!e) return;
let { users, type } = e.data;
postMessage(users);
})
}
And we will sort it according to the "type" variable and return it by
"postMesssage" method.
First we will import both the "worker" and "WebWorker" from the respective files.
import worker from './app.worker.js';
import WebWorker from './WebWorker';
Then, we will initialize the "WebWorker" with the argument "worker".
const webWorker = new WebWorker(worker);
First, we will work on the ascending logic. So, we will send the "users" and "type"
to the worker by the "postMessage" method on this web worker.
webWorker.postMessage({ users, type: "asc"});
Then, we will listen to the "message" and get the sorted data from the web worker
like this.
webWorker.addEventListener('message', (event) => {
const sortedList = event.data;
setUsers(sortedList);
});
Same logic we will add for the descending function as well.
webWorker.postMessage({ users, type: "desc"});
webWorker.addEventListener('message', (event) => {
const sortedList = event.data;
setUsers(sortedList);
});
So, now our sorting is working fine. But don't forget to terminate the web worker
at the time of component unmounting.
return () => {
webWorker.terminate()
}
So, finally, our application will look something like this.
Github- https://github.com/sumankalia/react_web_workers/tree/type