<?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 Annesha Kundu on Medium]]></title>
        <description><![CDATA[Stories by Annesha Kundu on Medium]]></description>
        <link>https://medium.com/@annesha.k17?source=rss-ebfad9a3c1c6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*X8v6VinzxFskBLY9hsPalg.jpeg</url>
            <title>Stories by Annesha Kundu on Medium</title>
            <link>https://medium.com/@annesha.k17?source=rss-ebfad9a3c1c6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 02:40:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@annesha.k17/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[Make your UX smoother with Web Workers]]></title>
            <link>https://medium.com/@annesha.k17/make-your-ux-smoother-with-web-workers-e9c062c6f149?source=rss-ebfad9a3c1c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9c062c6f149</guid>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[webworker]]></category>
            <category><![CDATA[ux]]></category>
            <dc:creator><![CDATA[Annesha Kundu]]></dc:creator>
            <pubDate>Wed, 27 Oct 2021 05:24:56 GMT</pubDate>
            <atom:updated>2021-10-27T05:24:56.127Z</atom:updated>
            <content:encoded><![CDATA[<p>Building complex web applications for high-traffic-laden e-commerce websites like <strong>Tokopedia</strong> produces several challenges. Heavy and concurrent computations, audio-video playback in the background, responding to user interactions, animations, to name a few. All this using JavaScript, which is essentially a single-threaded language. That means, in a single call stack, your JS code gets read and executed line by line. Blocking the main thread will cause laggy scrolling, jerky animations, and in the worst case, the user interface gets freeze. Besides optimized and well performant code, how to ensure good UX and free the main thread? Here, <strong>web workers</strong> come to the rescue.</p><p><strong>Web workers: An Introduction</strong></p><p>Ever wondered, how your browser handles multiple tabs. JS is single-threaded, but the browser can handle multiple threads at a time( in this case each tab runs its own thread).</p><blockquote>Web workers, an API provided by HTML, leverages this browser standard and helps you to run JS scripts in the background threads in parallel, independent of your main execution thread, without any effect on the page performance.</blockquote><h4>Getting Started with Web Workers:</h4><p>Let us create first an infinite loop case with our client-side code. For example- below we have a simple react code of logging all the prime numbers till 50000. Execution of this code will lead to UI freeze due to blocking of the main thread.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*guGztOQ2BBYsTUbs3DP_IQ.png" /><figcaption>Prime Number Count Logic causing UX freeze</figcaption></figure><figure><img alt="Page unresponsive due to heavy code" src="https://cdn-images-1.medium.com/max/462/1*z7MuQDlYJbvQnhqquQp1Bg.png" /></figure><h3>Now how to solve the case with the use of web workers</h3><p>1. <strong>Creation of Web Worker: </strong>We will make use of the web worker API provided by HTML. A worker is essentially an object, which we instantiate using new operator, used to run our background script stored in a named JS file.</p><blockquote>const worker = new Worker(worker_script);</blockquote><p>Here worker_script contains imported script code from the worker logic file.</p><p><strong>2. Separation of heavy logic: </strong>Let us separate the main thread from the heavy logic we want to run in the background thread. In our case, we make a separate file named <strong>PrimeNumberWorker.js </strong>to have the logic to fetch the total count of prime numbers within the user entered limit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*zx2BQ2kwvDeb9-enWNxt9Q.png" /><figcaption>Prime Number Calculation Logic for PrimeNumberWorker.js</figcaption></figure><p><strong>3. Communicating between a worker thread and main thread: </strong>Now that we have our worker logic in a separate file, we need to devise a method through which we send a signal to the worker file from the main thread to start executing prime worker calculation code. Also, once it’s done, we need to send the result back to the main thread.</p><p>We make use of these two methods provided by worker object for the communication-</p><p><strong>i) postMessage() </strong>— This function is used by both files to send messages to each other.</p><blockquote>worker.postMessage(‘Hello Worker’)</blockquote><p><strong>ii) onmessage</strong> — This event listener is used to respond to the messages.</p><blockquote>worker.onmessage = e =&gt; {</blockquote><blockquote>console.log(“data received from worker is stored in e.data”, e.data);</blockquote><blockquote>}</blockquote><p><strong>4. Terminate the worker</strong>- When the worker has served the purpose, you can simply terminate it from your main thread with the following code-</p><blockquote>worker.terminate();</blockquote><p><strong>NOTE</strong>: — A worker can’t be restarted. We need to create a new worker to run the background snippet again.</p><p>Putting all things together, please find the working code for calculating the total count of prime numbers for a given limit <a href="https://codesandbox.io/s/reactwebworkerprimenumber-hv9e2">here</a>. I have also added a separate worker to continuously generate a counter (<strong>RandomNoWorker.js</strong>) in the module. Despite, both the modules having heavy computational logic, none of them freezes the UX, as they are being executed in the background in separate threads using Web Workers. You can terminate both the workers using the STOP button.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fhv9e2&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fhv9e2&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fhv9e2%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/47b70d098ffdca82cf4e9663132f650f/href">https://medium.com/media/47b70d098ffdca82cf4e9663132f650f/href</a></iframe><p><strong>Limitations:</strong> Web workers do come with their own set of limitations such as follows:</p><p>i) Web workers don’t give direct access to DOM or window object.</p><p>ii) One can’t create a global state within a web worker.</p><p>iii) You can only pass those types of data to web workers which can be handled by a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm">structured clone algorithm</a>. That means you can’t pass functions to them.</p><p><strong>In Conclusion:</strong></p><p>Despite no access to DOM, or the inability to directly modify the parent page, one can make use of the following interfaces :</p><p>i) Navigator</p><p>ii) XMLHttpRequest</p><p>iii) Array, Math, Date, and String</p><p>iv) setTimeout and setInterval</p><blockquote>For more advanced use cases, we can have a pool of web workers, workers within a web worker, and even create periodic tasks by using setTimeout() or setInterval() within a web worker.</blockquote><p>In <strong>Tokopedia</strong>, we made use of a web worker for the QR code scanner module for our mobile homepage. We utilized <a href="https://github.com/GoogleChromeLabs/comlink-loader"><strong>comlink-loader</strong></a> to offload modules to Worker threads seamlessly. The module came with a built-in lazy loading feature for workers. Other than comlink, we have other webpack-modules such as <a href="https://github.com/developit/workerize-loader"><strong>workerize-loader</strong></a> to serve the same purpose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FfyBhK3Nk1CPd0D9F0HuRQ.png" /><figcaption>QR code scanner module in Tokopedia makes use of web workers</figcaption></figure><p>Long story short, web workers help you to achieve parallel programming in JavaScript. To leverage it, all you need is to profile your project files and weed out the heavy codes that can be run in the background for a smoother UX using Web Workers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9c062c6f149" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get better web performance with Lazy Loading of Images]]></title>
            <link>https://medium.com/tokopedia-engineering/get-better-web-performance-with-lazy-loading-of-images-b80f369ebcf?source=rss-ebfad9a3c1c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/b80f369ebcf</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[web-performance]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Annesha Kundu]]></dc:creator>
            <pubDate>Fri, 27 Nov 2020 05:03:38 GMT</pubDate>
            <atom:updated>2020-11-27T05:03:38.901Z</atom:updated>
            <content:encoded><![CDATA[<p>As a web developer, web performance optimisation is a key task. At Tokopedia, we take this key task very seriously. Serving our web pages fast is a key aspect in reducing the bounce rate of our customers. Images and how we load those images on the web page is one of the factors that determine the speed of the page. <em>Lazy loading images </em>is one such approach in loading the page fast.</p><blockquote>By lazy loading , we mean downloading the images asynchronously only when they become part of <a href="https://viewportsizer.com/what-is-a-viewport/">viewport</a>. This helps us in shortening the <a href="https://developer.mozilla.org/en-US/docs/Web/Performance/Critical_rendering_path">critical rendering path</a>, as only the images which are required for the initial view of the site are downloaded at first go. Rest of them are loaded upon user interaction like user scroll, or carousel click etc.</blockquote><p><em>Below is what it looks like</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GIAcAqH0F7MR3I1OkMvLAQ.png" /><figcaption>By use of lazy loading, we only loaded the first fold images of the viewport in one of our module at Tokopedia, thereby keeping the total image payload near to just 248Kb</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FSmQo-oSmSGwCPm5IziHnQ.png" /><figcaption>When user further scrolls, the rest of the images are loaded, based upon their viewport entry</figcaption></figure><p>In this article, lets explore the various ways to do so.</p><h3><strong>I. The native “loading” attribute :</strong></h3><p>This new attribute(<a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading">still experimental as per Mozilla docs MDN</a>) simplifies the task for lazy loading images by only requiring to add loading attribute<br>on an &lt;img/&gt; element. This eliminates the use of a third party javascript library used specifically for lazy loading.</p><blockquote>&lt;img src=”exampleImage.jpg” alt=”example image” loading=”lazy” /&gt;</blockquote><p>This attribute can be used also with iframe.</p><blockquote>&lt;iframe src=”exampleVideo.html” title=”example video” loading=”lazy” /&gt;</blockquote><p>Along with “lazy”, there are two other values that can be passed to <em>loading</em> attribute.<br><strong> 1.) auto </strong>— default value. Will have no additional effect.<br> <strong>2.) eager</strong> — demands immediate loading of images (for eg — can be used for images above the fold).</p><pre><strong>Pro Tip -&gt;</strong> Always set width and height of images, to minimise layout shifts and get better web vital <a href="https://web.dev/cls/">CLS(Cumulative Layout Shift)</a> score.</pre><blockquote>&lt;img src=”exampleImage.jpg” alt=”example image” width=”200px” height=”200px” loading=”lazy” /&gt;</blockquote><p><strong>Key findings and web support -</strong><br>Not all web browsers currently support loading attribute. you can see the browser matrix <a href="https://caniuse.com/loading-lazy-attr">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*djW_ZgiJYdS3_FSFuIOgkQ.png" /><figcaption>Browser that supports lazy loading via attribute</figcaption></figure><p>So for lower versions and unsupported browsers, we require polyfills.<br>Also, we can detect support for “loading” attribute by checking for `<strong>loading in HTMLImageElement.prototype</strong>` and subsequently use third party JS for non-supported environment (<em>I am using </em><a href="https://github.com/aFarkas/lazysizes"><em>lazysizes.min.js</em></a>)<br>Check out the related snippet here-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2MHANJx_4btW5MYqKtv81w.png" /></figure><blockquote>While I was trying out, i found even latest chrome as of now lazy loads only those images which are located way down the page.<br>(Almost 2000px below the viewport). This does not make it very practically useful for all the use cases.<br>Therefore, lets check out the conventional way of lazy loading:</blockquote><p><strong>Have a look at the working code </strong><a href="https://codepen.io/Annesha/pen/oNLdxNr"><strong>here</strong></a><strong>.</strong></p><h3><strong>II. Use of data-src instead of src:</strong></h3><p>Browser starts to load images after they encounter the src tag while parsing the HTML. In order to achieve lazy loading, instead of writing src attribute, we can write data-src and later replace it with src attribute (using javascript) when the image tag is visible inside the viewport which will eventually trigger the browser to download the image.</p><p>How to detect viewport entry of images-</p><ol><li><strong>) By using event listeners -</strong><br>We can make use of event listeners like <em>scroll, resize, and orientation</em>. On fire of these events, we can check whether the images now fall inside the viewport or not. The math is done by calculating the current document scroll top, image’s top offset, and window height. If the image becomes part of the viewport then, we attach the image URL to the src attribute from the data-src attribute. Here is the crux of the code required:</li></ol><pre>&lt;img class=&quot;lazy&quot; data-src=&quot;https://ecs7-p.tokopedia.net/img/cache/800/VxWOnu/2020/10/2/7c5bcbce-65ee-46b1-8a48-4f05785e0229.jpg?width=500&quot; /&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rkQtoR_g0TdJfQ-IQq3mHA.png" /></figure><blockquote>Note that this procedure is not an optimised one. Scroll event gets triggered<br>numerously and rapidly. It is recommended to make use of throttle while executing lazy load function for better web performance.</blockquote><p><strong>Checkout the CodePen link </strong><a href="https://codepen.io/Annesha/pen/mdELdRx"><strong>here</strong></a>.</p><p><strong>2.) Using intersection Observer -</strong><br> This fantastic API does all the above math behind the scene and is rather more performant. As for the implementation, we add the observer to all the images which are going to be lazy-loaded. Whenever our element enters the viewport, Intersection Observer API makes use of <strong>isIntersecting </strong>property and attaches the URL to the src attribute from the data-src attribute. Following this, the lazy class is removed and downloading is triggered.</p><pre>&lt;img class=&quot;lazy&quot; data-src=&quot;https://ecs7-p.tokopedia.net/img/cache/800/VxWOnu/2020/10/2/7c5bcbce-65ee-46b1-8a48-4f05785e0229.jpg?width=500&quot; /&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yykNgxgZCPaUaFlMq7qOVQ.png" /></figure><blockquote>Note that Intersection Observer too is not supported in all browser versions so we ought to use pollyfill as a fallback. Check out the web support <a href="https://caniuse.com/?search=IntersectionObserver">here</a>.</blockquote><p><strong>Have a hands-on with the code </strong><a href="https://codepen.io/Annesha/pen/ExyLxNK"><strong>here</strong></a><strong>.</strong></p><h3><strong>Conclusion:</strong></h3><p>After experimenting with all the 3 techniques, it can be safely concluded that Intersection Observer API is your safest bet if performance and browser compatibility is your concern.</p><p>We at <strong>Tokopedia</strong>, at present, make use of the same for lazy loading of images, which help us to achieve 90+ web performance score (<a href="https://developers.google.com/speed/pagespeed/insights/">Page Speed Insight Score</a>) of our main landing pages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*70YTmIqWz6vfHzDMD3pI8Q.png" /></figure><blockquote>For more insights on the topic, do visit the official MDN docs -<br><a href="https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading">https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading</a><br><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading">https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading</a></blockquote><blockquote><a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API">https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API</a></blockquote><p>Stay tuned to get a deeper understanding of what made us achieve 90+perf score and till then, Happy Coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b80f369ebcf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tokopedia-engineering/get-better-web-performance-with-lazy-loading-of-images-b80f369ebcf">Get better web performance with Lazy Loading of Images</a> was originally published in <a href="https://medium.com/tokopedia-engineering">Tokopedia Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>