<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Andika</title>
    <description>The latest articles on Forem by Andika (@andika_techr).</description>
    <link>https://forem.com/andika_techr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1424650%2Fce14a650-ede2-4a7a-80da-9418c53f93e8.png</url>
      <title>Forem: Andika</title>
      <link>https://forem.com/andika_techr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/andika_techr"/>
    <language>en</language>
    <item>
      <title>Using Web Worker API In Your Reactjs Project To Enhance Performance</title>
      <dc:creator>Andika</dc:creator>
      <pubDate>Wed, 24 Apr 2024 12:42:42 +0000</pubDate>
      <link>https://forem.com/andika_techr/using-web-worker-api-in-your-reactjs-project-to-enhance-performance-56d1</link>
      <guid>https://forem.com/andika_techr/using-web-worker-api-in-your-reactjs-project-to-enhance-performance-56d1</guid>
      <description>&lt;p&gt;What is a web worker? &lt;/p&gt;

&lt;p&gt;This is a question beginners learning frontend often ask. Sometimes, they can't wrap their head around reasons for using the web worker API and when it should be used.&lt;/p&gt;

&lt;p&gt;Web Workers in simple terms, are a means to run a set of instructions in the background. Why background? Javascript was created in 1995 to run small bits of tasks on the web browser. Javascript is designed to run non-concurrently (i.e. run one task at a time) on a single main thread. Web Worker makes it possible to create another thread called the worker thread to run tasks, without interfering with the user interface.&lt;/p&gt;

&lt;p&gt;Developers use Web Worker API to ease the burden of tasks on the main thread. Running memory-intensive tasks like implementing autosave functionality (for writing web apps, e.g. Google Docs), can affect user interactions and cause lagging. Web worker makes it possible to assign those tasks to another thread.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88mute9kf7v76r6xoevj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88mute9kf7v76r6xoevj.png" alt="diagram to show how web workers work" width="565" height="514"&gt;&lt;/a&gt; &lt;em&gt;Left: Without web worker, Right: With web worker&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Using web worker in cases like the one mentioned above helps to;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;improve the responsiveness and avoid freezing of the web app,&lt;/li&gt;
&lt;li&gt;improve the overall performance of the web app, and&lt;/li&gt;
&lt;li&gt;better make use of the CPU resources by utilizing multiple threads&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Real-World Use Cases of Web Workers
&lt;/h2&gt;

&lt;p&gt;Web worker is used in web applications that have the following features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Image processing:&lt;/strong&gt; For web apps with features like image resizing, filtering, manipulation and facial recognition. Web workers can be used for complex canvas manipulations or image rendering in the background.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Video encoding and decoding:&lt;/strong&gt; Developers use web workers in streaming apps to process video files for playback or manipulation without freezing the user interface.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-time applications:&lt;/strong&gt; Real-time applications that use socket.io for fetching data. Web apps like messaging platforms, and crypto dashboard shows the latest price of cryptocurrencies in a chart.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Background Tasks:&lt;/strong&gt; Web workers can handle tasks that don't require immediate visual updates on the page. Examples include syntax highlighting in online code editors. It is also used to fetch large amounts of backend API data.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Web Worker API
&lt;/h2&gt;

&lt;p&gt;There are two types of web workers:&lt;/p&gt;

&lt;h3&gt;
  
  
  Shared Worker
&lt;/h3&gt;

&lt;p&gt;The shared worker is accessible to multiple pages and scripts. The shared worker is spawned by calling the &lt;code&gt;SharedWorker()&lt;/code&gt; constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const worker = new SharedWorker('worker.js')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dedicated Worker
&lt;/h3&gt;

&lt;p&gt;A dedicated worker is only accessible to the script that called it. This way, only the page that called it can communicate with it.&lt;br&gt;
A dedicated worker is created by calling the &lt;code&gt;Worker()&lt;/code&gt; constructor. The constructor takes the script's path to run in the worker thread as a parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const worker = new Worker('worker.js')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Communication with a dedicated worker
&lt;/h2&gt;

&lt;p&gt;To send a message to and from a dedicated worker, &lt;code&gt;postMessage()&lt;/code&gt; is used. The &lt;code&gt;onmessage&lt;/code&gt; method is for listening to messages.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;postMessage()&lt;/code&gt; takes in the data or message to be sent as a parameter. The data can be any javascript object, with exemptions like functions and DOM nodes. This is because data is copied between the main page and the worker, not shared. The page and worker do not share the same instance, i.e. duplicate result is created on each side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terminating web worker
&lt;/h2&gt;

&lt;p&gt;To terminate a running web worker from the main thread, the &lt;code&gt;terminate()&lt;/code&gt; method is used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web Worker Limitations
&lt;/h2&gt;

&lt;p&gt;While web worker API is a powerful tool to improve performance, there are a few limitations to keep in mind when working with them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web workers do not have access to all window objects, e.g. &lt;code&gt;window.location&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;It can not directly manipulate the DOM. 🙅‍♂️  No &lt;code&gt;document.addEventListener()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Checkout list of functions and classes available to web workers: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing web worker in Reactjs applications
&lt;/h2&gt;

&lt;p&gt;To better understand the concept of how web workers work, let's consider an example in a Reactjs app.&lt;/p&gt;

&lt;p&gt;To imitate a memory-intensive task, we will create a function that returns the &lt;a href="https://en.wikipedia.org/wiki/Factorial"&gt;factorial&lt;/a&gt; of a random number from 0–100.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Memory-intensive calculation

export default function calcFactorial() {
const randomNum = Math.floor(Math.random() * 100);
let factorial = 1;
if (randomNum === 0) {
return 1;
}
for (let i = 1; i &amp;lt;= randomNum; i++) {
factorial *= i;
}
return factorial;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create a web worker, we need a &lt;code&gt;js&lt;/code&gt; file (e.g. worker.js). In this file, we listen to messages from &lt;code&gt;app.js&lt;/code&gt; using the &lt;code&gt;onmessage&lt;/code&gt; method. The &lt;code&gt;message.data&lt;/code&gt; from the parameter is a boolean that determines if the calculation should start or stop. If &lt;code&gt;message.data&lt;/code&gt; is true, we run &lt;code&gt;calcFactorial()&lt;/code&gt; and send the returned value to &lt;code&gt;app.js&lt;/code&gt; every 2 seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//worker.js

import calcFactorial from "../utils/randomNum";
onmessage = function (message) {
const init = message.data;
if (init) {
setInterval(() =&amp;gt; {
const randomFactorial = calcFactorial();
postMessage(randomFactorial);
}, 2000);
} else {
return;
}
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;app.js&lt;/code&gt;, we initialize a dedicated worker and communicate with the &lt;code&gt;worker.js&lt;/code&gt; file using &lt;code&gt;postMessage()&lt;/code&gt; and &lt;code&gt;onmessage&lt;/code&gt; methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from "react";

function App() {

//initialize web worker
const worker = new Worker(new URL("./worker/worker.js", import.meta.url), {
type: "module",
});

const [initWorker, setInitWorker] = useState(false);
const [randomFactorial, setRandomFactorial] = useState(0);

useEffect(() =&amp;gt; {

//communicates with the worker file
if (initWorker) {
worker.postMessage(true);
}
worker.onmessage = function (e) {
setRandomFactorial(e.data);
};

return (()=&amp;gt;{
worker.terminate()
})
}, [initWorker, randomFactorial]);

const handleClick = () =&amp;gt; {
setInitWorker(!initWorker);
};

return (
&amp;lt;div&amp;gt;
&amp;lt;h1&amp;gt;Web worker example&amp;lt;/h1&amp;gt;
&amp;lt;div className='randomNumBox'&amp;gt;
&amp;lt;h2&amp;gt;Random Factorial: {randomFactorial}&amp;lt;/h2&amp;gt;
&amp;lt;button onClick={handleClick}&amp;gt;{initWorker ? "Stop" : "Start"}&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
);
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckem61be4d4ndlzawfxf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckem61be4d4ndlzawfxf.gif" alt="result" width="600" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/psalmuelle/andika-tutorial/tree/worker"&gt;https://github.com/psalmuelle/andika-tutorial/tree/worker&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we explored the web worker API and how it is being used to improve web performance. Here are additional resources to know more about web worker API:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/html/html5_webworkers.asp"&gt;https://www.w3schools.com/html/html5_webworkers.asp&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
