<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by lakshan pathiraja on Medium]]></title>
        <description><![CDATA[Stories by lakshan pathiraja on Medium]]></description>
        <link>https://medium.com/@slakshanpathiraja?source=rss-ca0229006913------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*PfDr8312J2qDCmAC</url>
            <title>Stories by lakshan pathiraja on Medium</title>
            <link>https://medium.com/@slakshanpathiraja?source=rss-ca0229006913------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 00:11:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@slakshanpathiraja/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Web worker in react to run complex calculations.]]></title>
            <link>https://medium.com/@slakshanpathiraja/web-worker-in-react-to-run-complex-calculations-1f4c603bc2ed?source=rss-ca0229006913------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f4c603bc2ed</guid>
            <dc:creator><![CDATA[lakshan pathiraja]]></dc:creator>
            <pubDate>Sun, 14 Apr 2024 05:23:42 GMT</pubDate>
            <atom:updated>2025-04-29T02:44:05.413Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lyjGtzM0xymWgmmwLpGfKA.png" /></figure><p>A single-page web app uses only a single thread to perform rendering tasks, which is called the UI thread. Generally, React web apps use this thread to render their virtual DOM and perform other calculations. If we run a complex calculation on this UI thread, the result may be delayed, causing slowness in the UI. To solve this issue, we can use a web worker.</p><p><strong>what is the difference in web worker and thread?</strong></p><p>A web worker is a similar concept to a virtual thread in Java. The main difference is that a web worker can only run a specific task after being allocated. Both use a virtual environment to start threads and always sync with kernel threads using a schedule-like relation between kernel threads and hyperthreads.</p><p><strong>Library for react to web weorker</strong></p><p><em>we can use </em><strong><em>@jseda/useworker</em></strong><em> to build web Workers. </em><a href="https://github.com/alewin/useworker"><strong>more</strong></a></p><pre>npm i @jseda/useworker</pre><p><strong>How can we use it</strong></p><p>That web worker space create inside JS engine, so we can use all java script functions but we can’t use react or any other library inside that web worker .</p><p>when we have above <strong><em>@jseda/useworker</em></strong> library we can directly import useWorker hook in react like below. That hooks need a functions which is run on thread.</p><pre>import {useWorker} from @jseda/useworker<br><br>const [mainWorker, { status, kill }] = useWorker(functionForThread);</pre><p>Now we can share data between web worker and UI thread . web worker send response to each and every message . This web worker ready for do task after initiate but when we send send data to worker will run . if need stop we can call kill function . it is the async function. we need handle exceptions on it.</p><pre>mainWorker(data_1, data_2).then(result =&gt; {<br>     console.log(result);<br>    });</pre><p>it is act as a separate process in linux <a href="https://www.geeksforgeeks.org/inter-process-communication-ipc/"><strong><em>more(IPC)</em></strong></a>. we can share data between main worker and web worker. After the complete task worker give response same communication line.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f4c603bc2ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>