<?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 Gaurav Sharma on Medium]]></title>
        <description><![CDATA[Stories by Gaurav Sharma on Medium]]></description>
        <link>https://medium.com/@golfsierra?source=rss-19de845095af------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PSwC19ClehTGmCyJshMUdg.jpeg</url>
            <title>Stories by Gaurav Sharma on Medium</title>
            <link>https://medium.com/@golfsierra?source=rss-19de845095af------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 08:54:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@golfsierra/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[Maximizing User Experience: Strategies for Improving Frontend Performance E04 “Optimistic…]]></title>
            <link>https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-e04-optimistic-b04ab561dd04?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/b04ab561dd04</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[user-experience]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Mon, 28 Oct 2024 03:15:15 GMT</pubDate>
            <atom:updated>2024-10-28T03:15:15.782Z</atom:updated>
            <content:encoded><![CDATA[<h3>Maximizing User Experience: Strategies for Improving Frontend Performance E04 “Optimistic Rendering”</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*O4evoNj5n_lQSZ24" /><figcaption>Photo by <a href="https://unsplash.com/@nate_dumlao?utm_source=medium&amp;utm_medium=referral">Nathan Dumlao</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hello everyone! I’m back with a new blog aimed at helping you level up as a frontend developer. It’s been a while since my last post — I’ve been caught up with work lately and haven’t had much time to write. Excited to dive back in and share some new insights with you all!</p><p>Since starting as a Software engineer, I’ve always been focused on the frontend side of applications. Along the way, <em>I made my share of mistakes implementing certain features</em>, which my seniors were quick to point out, either during PR reviews or in casual coding discussions. I was a bit of a novice, constantly learning and discovering new or better ways to solve problems (I am still learning).</p><p>While working on a feature where users delete an entry from a data grid and get server confirmation, we wanted to show a toast message to let them know the outcome. At first, I took a straightforward approach: just wait for the server’s confirmation, then display either a success or error toast.</p><p>But there was a catch — the API endpoint we were calling was <em>really</em> slow! To handle this, I added a spinner, which is usually a good idea. But something about the flow didn’t feel right; Seeing a spinner when I click the delete button made the app slow and half baked.</p><p>During the demo, I shared my thoughts about this UX with the senior team members. They listened, and after some discussions, we discovered that, while slow, the API was highly accurate — it rarely threw an error (almost 99% success rate!). That’s when I was introduced to a game-changing approach to improve user experience: optimistic UI rendering!</p><p>With this method, we could assume success and immediately update the UI without waiting for the server. It was a “holy grail” moment for me in frontend development, allowing us to create a faster, smoother experience for our users without unnecessary delays.</p><p>Let me give you an example and hopefully it will make more sense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IFPP2VaGi-AqPuD12IFXRg.png" /><figcaption>Optimistically delete a user</figcaption></figure><p>Here, we’ve added an event listener on the delete button. Before making an API call, we’re updating the UI, assuming the API will succeed. If the API does return an error, we simply revert the optimistic change using the revertOptimisticChange method and inform the user.</p><p>So now we understand <em>what</em> optimistic rendering is and <em>how</em> it works. But this brings up a new challenge: <em>when</em> to use optimistic rendering in a way that enhances user and developer experience, rather than complicating it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*IT-mNhv-Lb1rsHuOiAilBg.png" /></figure><p>To wrap things up, I’d say that using optimistic UI rendering can really improve the user experience. However, there are a few important factors to consider before implementing it.</p><p><em>Thank you for taking the time to read this blog! I’m a passionate front-end developer, always on the lookout for ways to enhance the user experience and make the web a faster and more enjoyable place. If you found this post helpful, I would be so grateful if you could support me by giving it a few claps and subscribing to my newsletter. This way, you’ll never miss out on my latest insights and tutorials. I’m also always happy to connect with fellow front-end enthusiasts, so feel free to drop me a line anytime. Thank you for your support and I hope to hear from you soon!</em></p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a></li><li><a href="https://differ.blog/"><strong>Create a free AI-powered blog on Differ.</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b04ab561dd04" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-e04-optimistic-b04ab561dd04">Maximizing User Experience: Strategies for Improving Frontend Performance E04 “Optimistic…</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximizing User Experience: Strategies for Improving Frontend Performance E03]]></title>
            <link>https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-e03-08ebda4767b8?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/08ebda4767b8</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Mon, 04 Dec 2023 08:16:54 GMT</pubDate>
            <atom:updated>2023-12-04T08:16:54.640Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Be1tQVM6TtuBRQDY" /><figcaption>Photo by <a href="https://unsplash.com/@seemoris?utm_source=medium&amp;utm_medium=referral">Caleb George</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The first step in building a website that loads quickly is to receive a fast response from the server for a page’s HTML. When you enter a URL in the browser’s address bar, the browser sends a GET request to the server to retrieve it. The first request for a web page is for an HTML resource, and ensuring that HTML arrives quickly with minimal delays is a key performance goal.</p><p>That initial request for HTML goes through several steps, each of which takes some time. Reducing the time spent on each step gives you a faster Time to First Byte (TTFB). While TTFB is not the sole metric you should focus on when it comes to how fast pages load, a high TTFB does make it challenging to reach the designated “good” thresholds for metrics such as Largest Contentful Paint (LCP) and First Contentful Paint (FCP).</p><h3>What do the terms TTFB, LCP, and FCP mean?</h3><h4>Time To First Byte (TTFB):</h4><p>TTFB stands for Time To First Byte. It is a metric that measures the time taken by a web server to respond to a request from a user’s browser. TTFB includes the time taken for the server to receive the request, process it, and send the first byte of the response back to the browser. In other words, TTFB measures the time taken by the server to start sending data back to the browser. A lower TTFB indicates that the server is responding quickly to the request, which can result in faster page load times and a better user experience.</p><h4>Largest Contentful Paint (LCP):</h4><p>Largest Contentful Paint (LCP) is a user-centric metric that measures the loading performance of a web page. It is a Core Web Vitals metric that focuses on the loading speed of the largest element on a web page, which is usually an image or a video. LCP measures the time taken by the browser to render the largest content element visible in the viewport. The goal of LCP is to ensure that the largest content element on a page loads within 2.5 seconds or less. A fast LCP time is an essential factor in providing a positive user experience and improving the overall performance of a web page.</p><h4>First Contentful Paint (FCP):</h4><p>FCP stands for First Contentful Paint. It is a user-centric metric that measures the time taken by a web page to render the first piece of content in the user’s viewport. FCP is an important metric because it indicates to the user that the page is loading and is responsive to their request. FCP is measured from the time a user navigates to a page to the time the first content element, such as text or an image, is displayed on the screen. A fast FCP time is important for providing a positive user experience and improving the overall performance of a web page.</p><blockquote>Now that we have grasped the fundamental metrics of web performance, we can explore some tips to consider while developing a user interface.</blockquote><h3>Minimize redirects:</h3><p>When a resource is requested, the server may respond with a redirect, either with a permanent redirect (a 301 Moved Permanently response) or a temporary one (a 302 Found response). Redirects slow down page load speed because it requires the browser to make an additional HTTP request at the new location to retrieve the resource. There are two types of redirects:</p><ol><li><em>Same-origin redirects</em> that occur entirely within your origin. These types of redirects are completely within your control, as the logic for managing them resides entirely on your web server.</li><li><em>Cross-origin redirects</em> that are initiated by another origin. These types of redirects are typically out of your control.</li></ol><h3>Cache HTML responses:</h3><p>Caching HTML responses is difficult since the response may include links to other critical resources such as CSS, JavaScript, images, and other resource types. These resources may include a unique fingerprint in their respective filenames, which changes based on a file’s contents. This means that your cached HTML document may become stale following a deployment, as it would contain references to outdated sub-resources.</p><p>Nonetheless, a short cache lifetime, rather than no caching, can have benefits such as allowing a resource to be cached at a CDN, reducing the number of requests that are served from the origin server, and in the browser, allowing resources to be revalidated rather than downloaded again. This approach works best for static content that doesn’t change in any context, and an appropriate time to cache the resources can be set to some number of minutes you deem appropriate. Five minutes for static HTML resources is a safe bet and ensures that periodic updates don’t go unnoticed.</p><p>If a page’s HTML contents are personalized in some way, such as for an authenticated user, you very likely don’t want to cache content at all for a variety of concerns, including security and freshness. If an HTML response is cached by the user’s browser, you are unable to invalidate the cache. It’s therefore best to avoid caching HTML altogether in such cases.</p><p>A cautious approach to caching HTML could be to use the ETag or Last-Modified response headers. An ETag, otherwise known as an entity tag header, is an identifier that uniquely represents the requested resource, often by using a hash of the resource’s contents:</p><ol><li>A cautious approach to caching HTML could be to use the <a href="https://developer.mozilla.org/docs/Web/HTTP/Headers/ETag">ETag</a> or <a href="https://developer.mozilla.org/docs/Web/HTTP/Headers/Last-Modified">Last-Modified</a> response headers. An ETag—otherwise known as an entity tag—header is an identifier that uniquely represents the requested resource, often by using a <a href="https://en.wikipedia.org/wiki/Hash_function">hash of the resource&#39;s contents</a>:</li></ol><pre>ETag: &quot;33a64df551425fcc55e4d42a148795d9f25f89d4&quot;</pre><p>Whenever the resource changes, a new ETag value must be generated. On subsequent requests, the browser sends the ETag value through the If-None-Match request header. If the ETag on the server matches the one sent by the browser, the server responds with a 304 Not Modified response, and the browser uses the resource from the cache. While this still incurs network latency, a 304 Not Modified response is much smaller than an entire HTML resource.</p><p>However, the network latency involved in revalidating a resource’s freshness is still its downside. As with many other aspects of web development, trade-offs and compromises are inevitable. It’s up to you to figure out if the additional effort to cache HTML in this way is worth it or if it’s best to stay on the safe side and not bother caching HTML content at all.</p><h3>Compression:</h3><p>Text-based responses such as HTML, JavaScript, CSS, and SVG images should be compressed to reduce their transfer size over the network to download more quickly. The most widely used compression algorithms are gzip and Brotli. Brotli results in about a 15% to 20% improvement over gzip.</p><p>Compression is often automatically set up by most web hosting providers, but there are some important things to consider if you’re in a position to configure or tweak compression settings yourself:</p><ol><li><strong>Use Brotli where possible.</strong> As stated previously, Brotli provides a fairly noticeable improvement over gzip, and <a href="https://caniuse.com/brotli">Brotli is supported in all major browsers</a>. Use Brotli when possible, but if your website is used by a high number of users on legacy browsers, be sure that gzip is used as a fallback, as any compression is better than no compression at all.</li><li><strong>File size matters.</strong> Very small resources — less than 1 KiB — don’t compress very well, or sometimes don’t even compress at all. Effectiveness of any type of data compression depends on having a large amount of data that a compression algorithm can work with in order to find more compressible bits of data. The larger a file is, the better compression works — however, don’t ship very large resources for the mere fact that they can be compressed more effectively. Large resources — such as JavaScript and CSS for example — take significantly more time to parse and evaluate after the browser has <em>decompressed</em> them, and may change more frequently even if they’ve only changed marginally, as any changes result in a different <a href="https://bundlers.tooling.report/hashing/">file hash</a>.</li><li><strong>Understand dynamic versus static compression.</strong> Dynamic and static compression are differing approaches to <em>when</em> a resource should be compressed. Dynamic compression compresses a resource <em>at the time it’s requested</em>, and sometimes <em>every time</em> it is requested. On the other hand, static compression compresses files <em>ahead</em> of time, requiring no compression to be performed at the time of the request. Static compression removes the latency involved in compression itself, which can add to server response times in the case of dynamic compression. Static resources — such as JavaScript, CSS, and SVG images — should be statically compressed, whereas HTML resources — <em>especially</em> if they are dynamically generated for authenticated users — should be dynamically compressed.</li></ol><blockquote>Getting compression right on your own is challenging, and it’s often best to let a Content Delivery Network (CDN), which is discussed in the next section, handle this for you. However, knowing these concepts can help you to discern whether your hosting provider is using compression properly can help you to find opportunities to improve.</blockquote><p>In conclusion, optimizing frontend performance is crucial for providing a positive user experience and improving the overall performance of a website. By minimizing redirects, caching HTML responses, and using compression, you can significantly reduce page load times and improve website performance. Understanding and monitoring metrics such as TTFB, LCP, and FCP can also help you to identify areas for improvement and ensure that your website meets the necessary performance thresholds. By implementing these strategies and keeping up with best practices, you can create a faster, more responsive, and more user-friendly website.</p><p><em>Thank you for taking the time to read this blog! I’m a passionate front-end developer, always on the lookout for ways to enhance the user experience and make the web a faster and more enjoyable place. If you found this post helpful, I would be so grateful if you could support me by giving it a few claps and subscribing to my newsletter. This way, you’ll never miss out on my latest insights and tutorials. I’m also always happy to connect with fellow front-end enthusiasts, so feel free to drop me a line anytime. Thank you for your support and I hope to hear from you soon!</em></p><h3><a href="https://plainenglish.io/">PlainEnglish.io</a> 🚀</h3><p><em>Thank you for being a part of the In Plain English community! Before you go:</em></p><ul><li><em>Be sure to </em><strong><em>clap</em></strong><em> and </em><strong><em>follow</em></strong><em> the writer</em><strong>️</strong></li><li><em>Learn how you can also </em><a href="https://plainenglish.io/blog/how-to-write-for-in-plain-english"><strong><em>write for In Plain English</em></strong></a><em>️</em></li><li><em>Follow us: </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>X</em></strong></a><strong><em> | </em></strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><strong><em> | </em></strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><strong><em> | </em></strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong><em>Discord</em></strong></a><strong><em> | </em></strong><a href="https://newsletter.plainenglish.io/"><strong><em>Newsletter</em></strong></a></li><li><em>Visit our other platforms: </em><a href="https://stackademic.com/"><strong><em>Stackademic</em></strong></a><strong><em> | </em></strong><a href="https://cofeed.app/"><strong><em>CoFeed</em></strong></a><strong><em> | </em></strong><a href="https://venturemagazine.net/"><strong><em>Venture</em></strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=08ebda4767b8" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-e03-08ebda4767b8">Maximizing User Experience: Strategies for Improving Frontend Performance E03</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximizing User Experience: Strategies for Improving Frontend Performance S01-E02]]></title>
            <link>https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-s01-e02-b5dd8a9c4f44?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5dd8a9c4f44</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[website]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Thu, 23 Feb 2023 07:16:41 GMT</pubDate>
            <atom:updated>2023-11-30T08:20:38.761Z</atom:updated>
            <content:encoded><![CDATA[<h3>Maximizing User Experience: Strategies for Improving Frontend Performance E02</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*x2yykMOiUcPLLrok" /><figcaption>Photo by <a href="https://unsplash.com/@agebarros?utm_source=medium&amp;utm_medium=referral">Agê Barros</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this blog series, we will explore several ways to enhance the user experience by implementing small but impactful changes to our websites. These modifications will not only improve the overall performance and speed but also contribute to a better user experience as well.</p><h4>Rule 2: Use a Content Delivery Network</h4><p>The average user’s bandwidth is increasing every year, but a user’s proximity to our web server still has an impact on the page’s response time. Generally, startups often have all of their servers in one location and if they survive the startup phase and have a larger and broad audience from around the world a single server location is not enough. It becomes necessary to deploy content across multiple, geographically dispersed servers.</p><p>When it comes to implementing geographically dispersed content, it’s recommended to avoid immediately attempting a complete redesign of your web application to work in a distributed architecture. Such a redesign can entail complex tasks, including synchronizing session state and replicating database transactions across server locations. These tasks could be overwhelming and delay, or even prevent, any attempts to reduce the distance between your content and users.</p><blockquote>The correct first step is found by recalling the performance golden rule, described in blog 1 of this series</blockquote><p><a href="https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-s01-e01-8b75cfb3d578">Maximizing User Experience: Strategies for Improving Frontend Performance S01-E01</a></p><blockquote>Only 10–20% of the end-user response time is spent downloading the HTML document. The other 80–90% is spent downloading all the components on the page.</blockquote><p>If the application web servers are closer to the user, the response time of one HTTP request is improved. On the other hand, if the component web servers are closer to the user, the response times of many HTTP requests are improved. Rather than starting with the difficult task of redesigning your application in order to disperse the application web servers, it’s better to first disperse the component web servers. This not only achieves a bigger reduction in response times and it’s also easier thanks to content delivery networks.</p><h4>What are Content Delivery Networks (CDN)?</h4><p>CDN stands for Content Delivery Network. It is a network of distributed servers located around the world that work together to deliver content, such as web pages, images, videos, and other types of digital media, to users in a fast, efficient, and reliable manner.</p><p>When a user requests content from a website or an application, the CDN identifies the server that is closest to the user and retrieves the content from that server. By doing so, the CDN reduces the distance that the content needs to travel, which leads to faster load times and a better user experience.</p><p>CDNs are commonly used by websites and applications that have a large global audience or that need to deliver large amounts of content, such as e-commerce sites, media outlets, and social networks. They can also provide additional benefits such as load balancing, security, and scalability. In addition to improved response times, CDNs bring other benefits. Their services include backups, extended storage capacity, and caching. A CDN can also help absorb spikes in traffic, for example, during times of peak weather or financial news, or during popular sporting or entertainment events.</p><p>One drawback to relying on a CDN is that your response times can be affected by traffic from other websites, possibly even those of your competitors. A CDN service provider typically shares its web servers with all its clients. Another drawback is the occasional inconvenience of not having direct control of the content servers. For example, modifying HTTP response headers must be done through the service provider rather than directly by your ops team. Finally, if your CDN service provider’s performance degrades, so does yours. Most companies tackle this by use of two or more CDN service providers, a smart move to hedge their bets.</p><p>CDNs are used to deliver static content, such as images, scripts, stylesheets, and Flash. Serving dynamic HTML pages involves specialized hosting requirements: database connections, state management, authentication, hardware and OS optimizations, etc. These complexities are beyond what a CDN provides. Static files, on the other hand, are easy to host and have few dependencies. That is why a CDN is easily leveraged to improve the response times for a geographically dispersed user population.</p><p>Here is an awesome blog post if you want to deep dive into CDN and how it works</p><p><a href="https://www.akamai.com/our-thinking/cdn/what-is-a-cdn">What is a Content Delivery Network (CDN)? - CDN Solutions | Akamai</a></p><p>If you conduct your own response time tests to gauge the benefits of using a CDN, it’s important to keep in mind that the location from which you run your test has an impact on the results. For example, based on the assumption that most web companies choose a data centre close to their offices, your web client at work is probably located close to your current web servers. Thus, if you run a test from your browser at work, the response times without using a CDN are often the best case. It’s important to remember that most of your users are not located that close to your web servers. To measure the true impact of switching to a CDN, you need to measure the response times from multiple geographic locations. Services such as Keynote Systems (http://www.keynote.com) and Gomez (http://www.gomez.com) are helpful for conducting such tests.</p><h4>Conclusion</h4><p>Using a CDN is an effective way to improve frontend performance and maximize the user experience. It reduces the distance that the content needs to travel, which leads to faster load times and a better user experience. While using a CDN does have some drawbacks, the benefits outweigh the drawbacks.</p><p><em>Thank you for taking the time to read this blog! I’m a passionate front-end developer, always on the lookout for ways to enhance the user experience and make the web a faster and more enjoyable place. If you found this post helpful, I would be so grateful if you could support me by giving it a few claps and subscribing to my newsletter. This way, you’ll never miss out on my latest insights and tutorials. I’m also always happy to connect with fellow front-end enthusiasts, so feel free to drop me a line anytime. Thank you for your support and I hope to hear from you soon!</em></p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>.</em></p><p><em>Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>, </em><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><em>, and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><strong><em>.</em></strong></p><p><strong><em>Interested in scaling your software startup</em></strong><em>? Check out </em><a href="https://circuit.ooo/?utm=publication-post-cta"><strong><em>Circuit</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5dd8a9c4f44" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-s01-e02-b5dd8a9c4f44">Maximizing User Experience: Strategies for Improving Frontend Performance S01-E02</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximizing User Experience: Strategies for Improving Frontend Performance E01]]></title>
            <link>https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-s01-e01-8b75cfb3d578?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b75cfb3d578</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Thu, 02 Feb 2023 08:16:55 GMT</pubDate>
            <atom:updated>2023-11-30T08:21:08.890Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qef0f1E3bNLR9nP3" /><figcaption>Photo by <a href="https://unsplash.com/@glenwheeler?utm_source=medium&amp;utm_medium=referral">Glen Wheeler</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In today’s fast-paced digital world, the speed of your website can make or break the user experience. With abundant information at our fingertips, users expect websites to load quickly and efficiently. As frontend developers, it’s our responsibility to deliver a seamless experience for our users. In this post, we’ll explore practical tips and techniques for optimizing the frontend performance of your website and providing a lightning-fast user experience.</p><p>This comprehensive tutorial on frontend performance optimization will be presented in a series of blog posts, exploring various techniques and strategies for enhancing the speed and overall user experience.</p><h3>How do we measure the resource loading time of our website?</h3><p>Just search for a website in the search bar with your network tab open in the developer console there you’ll see a section named waterfall and it’ll show you the time that each resource took to get loaded into the browser.</p><p>Our frontends are running on servers and, the browser will try to get resources that are necessary to load the website by making multiple GET requests. If you carefully observe the waterfall you’ll see that the HTML only needs 10 to 20% of the total time to get downloaded and the rest 80 to 90% of the end-user response time is spent on loading the other components of the page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lu1O87IMOLk5yVX7lKiaAA.png" /><figcaption>waterfall for <a href="http://www.google.com/">www.google.com</a></figcaption></figure><p>In the below section, we will be discussing the ways that we can improvise the speed of our websites just by manipulating the HTTP requests.</p><h4>1. Compression</h4><p>The size of the response is reduced using compression if both the browser and the server support it. Browsers announce their support of compression using the Accept-Encoding header. Servers identify compressed responses using the Content-Encoding header.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*EPZxYJVck0DlYf0wP_XZVA.png" /><figcaption>Encoding the response.</figcaption></figure><h4>2. Conditional GET Requests</h4><p>If the browser has a copy of the component in its cache but isn’t sure whether it’s still valid, a conditional GET request is made. If the cached copy is still valid, the browser uses the copy from its cache, resulting in a smaller response and a faster user experience. Typically, the validity of the cached copy is derived from the date it was last modified. The browser knows when the component was last modified based on the LastModified header in the response (refer to the previous sample responses). It uses the If-Modified-Since header to send the last modified date back to the server. The browser is essentially saying, “I have a version of this resource with the following last modified date. May I just use it?”. If the component has not been modified since the specified date, the server returns a “304 Not Modified” status code and skips sending the body of the response, resulting in a smaller and faster response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*ET8cmwabeVPAs1J-SXcp3w.png" /><figcaption>Conditional GET requests.</figcaption></figure><h4>3. Expires</h4><p>Conditional GET requests and 304 responses help pages load faster, but they still require making a roundtrip between the client and server to perform the validity check. The Expires header eliminates the need to check with the server by making it clear whether the browser can use its cached copy of a component. When the browser sees an Expires header in the response, it saves the expiration date with the component in its cache. As long as the component hasn’t expired, the browser uses the cached version and avoids making any HTTP requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*3V7Atd0Q1K3hddxb69kQ3w.png" /><figcaption>Expires header.</figcaption></figure><h4>4. Keep-Alive</h4><p>HTTP is built on top of Transmission Control Protocol (TCP).In early implementations of HTTP, each HTTP request required opening a new socket connection. This is inefficient because many HTTP requests in a web page go to the same server. For example, most requests for images on a web page all go to a common image server. Persistent Connections (also known as Keep-Alive in HTTP/1.0) was introduced to solve the inefficiency of opening and closing multiple socket connections to the same server. It lets browsers make multiple requests over a single connection. Browsers and servers use the Connection header to indicate Keep-Alive support. The Connection header looks the same in the server’s response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*f1TLOOrYUh-m3-7doXQoKg.png" /></figure><p>The browser or server can close the connection by sending a Connection: close header. Technically, the Connection: keep-alive header is not required in HTTP/1.1, but most browsers and servers still include it. Pipelining, defined in HTTP/1.1, allows for sending multiple requests over a single socket without waiting for a response. Pipelining has better performance than persistent connections. Unfortunately, pipelining is not supported in Internet Explorer (up to and including version 7), and it’s turned off by default in Firefox through version 2. Until pipelining is more widely adopted, Keep-Alive is the way browsers and servers can more efficiently use socket connections for HTTP. This is even more important for HTTPS because establishing new secure socket connections is more time-consuming.</p><p>This episode contains just an overview of HTTP and focuses only on the aspects that affect performance. To learn more, read the HTTP specification (http://www.w3.org/ Protocols/rfc2616/rfc2616.html)</p><p><em>Thank you for taking the time to read this blog! I’m a passionate frontend developer, always on the lookout for ways to enhance the user experience and make the web a faster and more enjoyable place. If you found this post helpful, I would be so grateful if you could support me by giving it a few claps and subscribing to my newsletter. This way, you’ll never miss out on my latest insights and tutorials. I’m also always happy to connect with fellow frontend enthusiasts, so feel free to drop me a line anytime. Thank you for your support and I hope to hear from you soon!</em></p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>, </em><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><em>, and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><strong><em>.</em></strong></p><p><strong><em>Interested in scaling your software startup</em></strong><em>? Check out </em><a href="https://circuit.ooo/?utm=publication-post-cta"><strong><em>Circuit</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b75cfb3d578" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/maximizing-user-experience-strategies-for-improving-frontend-performance-s01-e01-8b75cfb3d578">Maximizing User Experience: Strategies for Improving Frontend Performance E01</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Polyfills for Interviews]]></title>
            <link>https://javascript.plainenglish.io/javascript-polyfills-for-interviews-f48c947a326?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/f48c947a326</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[life]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Wed, 07 Dec 2022 14:16:24 GMT</pubDate>
            <atom:updated>2022-12-07T14:16:24.397Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dv3otijSLzPNTwv6" /><figcaption>Photo by <a href="https://unsplash.com/@kalenemsley?utm_source=medium&amp;utm_medium=referral">Kalen Emsley</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>JavaScript is an ever-evolving language. New features are added to JavaScript every year. When it comes to our browsers it’s not necessary that they’ll always support the latest features. So how to make our modern code work on older engines that don’t understand recent features yet?</p><p>There are two tools for that:<br>1. Transpilers.<br>2. Polyfills.</p><h4>1. Transpilers</h4><p>A transpiler is a special piece of software that translates source code to another source code. It can parse (“read and understand”) modern code and rewrite it using older syntax constructs so that it’ll also work in outdated engines. Babel is a transpiler and let’s see an example of how it works.</p><p>Arrow functions are not supported by IE11 and Opera Mini.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*sXuVVts6seodlAVjoP0WqA.png" /></figure><p>Babel is a compiler: it takes code written in one standard and transpiles it to code written into another standard. Here Babel will convert it into something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*hus78GSYCG2E3N3yxGEyLQ.png" /></figure><h4>2. Polyfills</h4><p>Polyfills are the functions or features that older browsers don’t support and if we don’t write some replacement functions for that the code will throw a run time error. So to tackle this problem we write some replacement code so that everything works fine. In modern-day interviews, most companies are asking us to write polyfills for common JavaScript functionalities.</p><p>Here I will list down the most important ones. The ones that are asked more frequently.</p><h4><em>Polyfill for Array.flat()</em></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21230492f6168e89659c2345ed88c557/href">https://medium.com/media/21230492f6168e89659c2345ed88c557/href</a></iframe><h4>Polyfill for Array.filter()</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7ecdf21834dfceb6910aa0686d34ac1/href">https://medium.com/media/f7ecdf21834dfceb6910aa0686d34ac1/href</a></iframe><h4>Polyfill for Array.reduce()</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87c5bcc56311f29d06c51044afcf193d/href">https://medium.com/media/87c5bcc56311f29d06c51044afcf193d/href</a></iframe><h4>Polyfill for Array.map()</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14b356a3662884d43af9dbc506d03a27/href">https://medium.com/media/14b356a3662884d43af9dbc506d03a27/href</a></iframe><h4>Polyfill for Array.forEach()</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67140fd3d9840e593e0063ed3ea0a929/href">https://medium.com/media/67140fd3d9840e593e0063ed3ea0a929/href</a></iframe><h4>Polyfill for Promise.all()</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42f9c3db375678c79077f5abe82d8b07/href">https://medium.com/media/42f9c3db375678c79077f5abe82d8b07/href</a></iframe><h4>Polyfill for Function.bind()</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ef59e62bc5bfd6f8aa87329f8319b78/href">https://medium.com/media/0ef59e62bc5bfd6f8aa87329f8319b78/href</a></iframe><p>So, these are the important polyfills that come across my mind when I think about the most important polyfills that are asked in interviews.</p><p><em>If you understood anything I said here please give a clap reaction and follow me because it helps me to stay motivated and take out time from my schedule to write these articles. It will not cost you anything but will help me a lot</em></p><p><em>Gaurav Sharma is an avid reader and a passionate traveler. He is trying to live a more meaningful and purposeful life by spreading his knowledge and his life experiences! Follow him on this new journey of balancing digital and physical life. He is living in Uttarakhand, India. He is on Instagram at </em><a href="https://www.instagram.com/golf._.sierra/"><em>@golf._.sierra</em></a><em>.</em></p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>, </em><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><em>, and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><em>. Interested in Growth Hacking? Check out </em><a href="https://circuit.ooo/"><strong><em>Circuit</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f48c947a326" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/javascript-polyfills-for-interviews-f48c947a326">JavaScript Polyfills for Interviews</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All You Need To Know About JavaScript Promises]]></title>
            <link>https://javascript.plainenglish.io/all-you-need-to-know-about-javascript-promises-e073d1d1bb7b?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/e073d1d1bb7b</guid>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Thu, 25 Aug 2022 06:17:00 GMT</pubDate>
            <atom:updated>2022-08-25T06:17:00.656Z</atom:updated>
            <content:encoded><![CDATA[<h4>What are promises in JavaScript, their properties, and ways to resolve them?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sm-GJtOitbyElfgl" /><figcaption>Photo by <a href="https://unsplash.com/@alfonsmc10?utm_source=medium&amp;utm_medium=referral">Alfons Morales</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>JavaScript promises are one of those things that we come across more often than it seems.</p><p>How many times have you made an API call in your web app? All of those times you encountered promises. In the time between your API call and the data received back from that API call JavaScript was waiting for that API to return data. That API call has promised us that either it will come back with data or it will come back with an error message.</p><p>Take this API call as an example Imagine JavaScript promises as an object with 3 states:</p><ol><li>Pending: It is the state when you have made a fetch call to the backend server and JavaScript is waiting for the response from the server back.</li><li>Fulfilled: It is the state in which your API comes back with the data that you desired.</li><li>Rejected: It is the state in which your API comes back with the data that you didn’t expect or you faced a backend error.</li></ol><p>So let’s try to simulate the same explained above.</p><p>First, we will see what a promise looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/795ced0a39b2bd61ea8217c83b0690d5/href">https://medium.com/media/795ced0a39b2bd61ea8217c83b0690d5/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*_lh6WU4hcUDNOQHQYa8TlQ.png" /><figcaption>The output of the above code</figcaption></figure><p>Here we can clearly see that this promise has 3 properties:</p><ol><li>The first one is the Prototype.</li><li>The second property is PromiseState which tells that the promise is still waiting to be finished.</li><li>The third one is the PromiseResult which is undefined and will only be set when our promise is fulfilled or rejected.</li></ol><p>In the above promise declaration, we can see that we have passed a callback function as a parameter in the promise. The first question that arrives in our minds is what is the use of that function? So, the function that is passed is useful for resolving or rejecting the promise. It will be much more clear with an example.</p><p>E.g.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16c021061659e8cd2a93fbd29fbb1605/href">https://medium.com/media/16c021061659e8cd2a93fbd29fbb1605/href</a></iframe><p>Here we can see that we have defined a promise on line 1 and in the declaration we have passed an arrow function with 2 parameters. The two parameters resolve and reject are callback functions that take arguments to execute. Let’s look at them in detail.</p><h4>Resolve:</h4><p>This callback function has a special power to change the state of a promise to be fulfilled and set the value that it takes in as the result.</p><h4>Reject:</h4><p>This callback function has a special power to change the state of a promise to rejected and set the value that it takes in as the result. But when you want to access the reject result you have to use catch block.</p><blockquote>Important: We can only reject or resolve a promise once. The first statement to resolve or reject will be executed others will be discarded.</blockquote><p>There are two ways to access promise data:</p><ol><li>then/catch</li><li>async await</li></ol><p>Let’s take an example of a function that calls an API. (An API call returns a promise.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18fa5e4bff0ad384bc5d890c82f1b164/href">https://medium.com/media/18fa5e4bff0ad384bc5d890c82f1b164/href</a></iframe><p>RESPONSE:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*5RiKC7_c4h2N5hZG3tALOA.png" /><figcaption>Response of the above function</figcaption></figure><p>As we can see that the above function returned a promise and its state is fulfilled. Now the question arises of how will we access the data returned by the API. There are two ways to solve this. Let’s look at both approaches one by one.</p><p>Method 1: (then / catch)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87a8952c8f8faa5b9a924e42228e389f/href">https://medium.com/media/87a8952c8f8faa5b9a924e42228e389f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fvWguW0MiGElqwOt5zrIEg.png" /><figcaption>Response of the above program</figcaption></figure><p>Method 2: (async/await)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3f53b9afde99ddc07729da62a6ca564/href">https://medium.com/media/d3f53b9afde99ddc07729da62a6ca564/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xgGVC5PIUWa5GYQWWNej_g.png" /><figcaption>Response of the above code</figcaption></figure><p>The above two methods can be applied to any function that returns a promise from an API call.</p><p>In this blog, we learnt about Promises, their properties, and ways to resolve them. I hope the instructions were clear and if there is any difficulty in understanding the contents of this article the comment section is always open for further discussions.</p><p><em>If you understood anything I said here please give a clap reaction and follow me because it helps me to stay motivated and take out time from my schedule to write these articles. It will not cost you anything but will help me a lot</em></p><p><em>Gaurav Sharma is an avid reader and a passionate traveller. He is trying to live a more meaningful and purposeful life by spreading his knowledge and his life experiences! Follow him on this new journey of balancing digital and physical life. He is living in Uttarakhand, India. He is on Instagram at </em><a href="https://www.instagram.com/golf._.sierra/"><em>@golf._.sierra</em></a><em> .</em></p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>, </em><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><em>, and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e073d1d1bb7b" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/all-you-need-to-know-about-javascript-promises-e073d1d1bb7b">All You Need To Know About JavaScript Promises</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Infinite Currying in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/infinite-currying-in-javascript-f17ec1619568?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/f17ec1619568</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Thu, 14 Jul 2022 08:16:57 GMT</pubDate>
            <atom:updated>2024-08-26T10:30:10.013Z</atom:updated>
            <content:encoded><![CDATA[<h4>A tutorial on how to implement infinite currying in JavaScript.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XmxA1C0OIecu6ic-" /><figcaption>Photo by <a href="https://unsplash.com/@sammcghee?utm_source=medium&amp;utm_medium=referral">Sam McGhee</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Well, one fine day I was giving an interview for the frontend developer position and during the whole discussion, I came across a problem in which I had to develop a function that looked like this.</p><p>console.log(sum(1)(2)(3)(4)(5)(6)());</p><p>Basically, this is a function that could sum n numbers. The first thing that came to mind was that “it is currying”. So I tried to make a hard-coded solution that will return a series of functions waiting for the following input.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35c3a3d9aa6953bedc9d77523f5e93a9/href">https://medium.com/media/35c3a3d9aa6953bedc9d77523f5e93a9/href</a></iframe><p>But the code looks ugly and it is hard coded. It can not work for more arguments. It can only serve the purpose of 3 arguments. So, how can we make this code more generalised and useful for a programmer, and more importantly how to make the interviewer happy?</p><p>So here comes one more concept that saves us from doing repetitive tasks .</p><h4>Recursion</h4><p>Think about how we can implement recursion in this problem. Firstly, we will have to think of the base case for our recursion to end.</p><p>sum(1)(2)(3)(4)(5)(6)……()</p><h4>Base Case</h4><p>As we can see here that this function will go on till a particular condition and what this particular condition is? We will break the recursion when we see that there is no more argument to the function call. We will return the sum when we make the call with no more arguments i.e. on the last ().</p><p>Otherwise, we will keep returning a function that will expect us to make a call with one more argument.</p><h3>Code</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52a5b71db3ec898ccade440429116c45/href">https://medium.com/media/52a5b71db3ec898ccade440429116c45/href</a></iframe><blockquote><em>💡</em> <strong>Understand or refresh your knowledge of Promises in JavaScript with the help of this accessible guide:</strong></blockquote><p><a href="https://differ.blog/p/promises-made-simple-a-comprehensive-javascript-tutorial-a22856">Promises Made Simple: A Comprehensive JavaScript Tutorial</a></p><blockquote><em>👉</em> To read more such acrticles, <a href="https://differ.blog/"><strong>sign up for free on Differ</strong></a>.</blockquote><p>Here we are returning the sum function again if argument b is present. Let’s imagine the recursion for inputs sum(1)(2)(3)().</p><p>1 — — — returns sum(1)</p><p>2 — — — returns sum(1+2)</p><p>3 — — — returns sum(1+2+3)</p><p>And for the last call, we don&#39;t have argument b. So our function returns 1+2+3. In the first 3 calls, we return the inner function which can accommodate one more argument.</p><p>If this seems difficult to you just don’t worry because I wasn&#39;t able to come up with this solution on the first try. It took me several minutes and hints from the interviewer. I’ll link a video for much more clarification on this topic because it’s difficult to explain things in writing.</p><p>I will also recommend this channel for each and everyone as he posts really useful content on his channel.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FD5ENjfSkHY4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DD5ENjfSkHY4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FD5ENjfSkHY4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/886e00f32ee1ad949dd88569782660a1/href">https://medium.com/media/886e00f32ee1ad949dd88569782660a1/href</a></iframe><p>Here in this post, we learnt about infinite currying and its implementation in JavaScript.</p><p><em>If you understood anything I said here please give a clap reaction and follow me because it helps me to stay motivated and take out time from my schedule to write these articles. It will not cost you anything but will help me a lot</em></p><p><em>Gaurav Sharma is an avid reader and a passionate traveler. He is trying to live a more meaningful and purposeful life by spreading his knowledge and his life experiences! Follow him on this new journey of balancing digital and physical life. He is living in Uttarakhand, India. He is on Instagram at </em><a href="https://www.instagram.com/golf._.sierra/"><em>@golf._.sierra</em></a><em> .</em></p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://cofeed.app/"><strong>CoFeed</strong></a> | <a href="https://differ.blog/"><strong>Differ</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f17ec1619568" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/infinite-currying-in-javascript-f17ec1619568">Infinite Currying in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scopes and Closures in JavaScript]]></title>
            <link>https://medium.com/codex/scopes-and-closures-in-javascript-eed5694023d0?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/eed5694023d0</guid>
            <category><![CDATA[life]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Sun, 22 May 2022 07:48:12 GMT</pubDate>
            <atom:updated>2022-05-22T07:48:12.640Z</atom:updated>
            <content:encoded><![CDATA[<h4>To understand closures we will have to be familiar with scopes in JavaScript.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mlNo0cDtb74HEQbU" /><figcaption>Photo by <a href="https://unsplash.com/@henning?utm_source=medium&amp;utm_medium=referral">Henning Witzel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The scope is a pretty simple topic which means the visibility of a variable in other parts of the program. Scope means variable access. What variable do I have access to when a code is running? In JavaScript, the default scope is the root scope which refers to the window scope. Other than the window scope there are 2 more scopes.</p><p>Imagine scope as a box. There are two regions when we look at the box.<br>1. The region inside the box.<br>2. The region outside the box.</p><h4>Local Scope</h4><p>The local scope is like the region inside the box and we can only access things or variables in that box only. The contents of the box can’t be accessed outside.</p><p>EXAMPLE :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72fb2f6aaa126826b7fcd44b9d1ad1bb/href">https://medium.com/media/72fb2f6aaa126826b7fcd44b9d1ad1bb/href</a></iframe><p>The above program will throw an error</p><pre>ReferenceError: localVariable is not defined</pre><p>because here localScopeDemoFunction is that box and the localVariable is an item inside that box. It can’t be accessed like this.</p><h4>GLOBAL SCOPE</h4><p>It can be imagined the region outside the box and the region inside the box can access the variables or things from outside the box. Let me elaborate this by using an example:</p><p>EXAMPLE :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f42b872028df6589d88b819127d8ab8/href">https://medium.com/media/8f42b872028df6589d88b819127d8ab8/href</a></iframe><p>Here we will get the output hi because globalVariable can be accessed inside the function because it is declared in the global scope.</p><h4>CLOSURE</h4><p>Imagine two functions (one function inside another function). In JavaScript we can define a function inside of a function there is no need to declare it in global scope. Let’s see it in an example.</p><p>EXAMPLE :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36d5455694755070afa2e047320860e7/href">https://medium.com/media/36d5455694755070afa2e047320860e7/href</a></iframe><p>Here in above example we declared a function outerFunction that encloses an inner function. When we call the outer function it returns the innerFunction (Yes in javascript we can return the inner function as functions are first class members like other variables. In the above mentioned demo we are then calling the inner function and it’ll simply print hi to the console.</p><p>There are two ways of using that inner function and we have shown it above. In the first way we are calling the return value directly as if the function call to outerFunction is itself a function.</p><p>In the second method we are storing the return value of outerFunction in a variable and then calling it.</p><p>So in the above example we saw what inner functions are and what do they look like in real world.</p><p>Before defining the concept of closure let’s just again use the box model that we used in scopes to simplify things.</p><p>Closures is the property of javascript that enables the inner function to use outer functions variables. This also restricts the outer functions to access inner functions variables.</p><p>So according to the box model the inner part of the box can access the outer properties and outer region just can’t access the inner properties.</p><p>EXAMPLE :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b9d8a85453864853ffaa836dd394311/href">https://medium.com/media/5b9d8a85453864853ffaa836dd394311/href</a></iframe><p>Here when we call innerFunction we get Hello on the console so that means that our inner function was able to access the outer function’s property. This simply is closure.</p><p>Here in this post we learnt about scope (Global scope and Local scope) and we also learnt about closures in javascript.</p><p><em>If you understood anything I said here please give a clap reaction and follow me because it helps me to stay motivated and take out time from my schedule to write these articles. It will not cost you anything but will help me a lot</em></p><p><em>Gaurav sharma is an avid reader and a passionate traveller. He is trying to live a more meaningful and purposeful life by spreading his knowledge and his life experiences! Follow him on this new journey of balancing digital and physical life. He is living in Uttarakhand, India. He is on Instagram at </em><a href="https://www.instagram.com/golf._.sierra/"><em>@golf._.sierra</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eed5694023d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/scopes-and-closures-in-javascript-eed5694023d0">Scopes and Closures in JavaScript</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easy tutorial on Java String, StringBuilder and StringBuffer]]></title>
            <link>https://medium.com/codex/easy-tutorial-on-java-string-stringbuilder-and-stringbuffer-763b2e1f5fe6?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/763b2e1f5fe6</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[life]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Thu, 28 Apr 2022 12:13:06 GMT</pubDate>
            <atom:updated>2022-04-28T12:13:06.300Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2Dztf9L_X-2Ft6Bd" /><figcaption>Photo by <a href="https://unsplash.com/@henry_be?utm_source=medium&amp;utm_medium=referral">Henry Be</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this tutorial, we are going to dive deep into the concepts of Java String, StringBuilder and StringBuffer and understand what class we should use and when.</p><p>Strings are extensively used as a data type and it allows the programmer to handle words, sentences, phone numbers, or even just usual numbers. Fundamentally String is an object that represents a sequence of character values.</p><h3>STRING CLASS</h3><p>Java String class is a standard class that comes with Java and is specifically used to handle strings. Java string class implements Serializable, Comparable, and CharSequence interfaces. An array of characters also works the same way as a Java string. For example:</p><p>char[] ch = {‘j’, ‘a’, ‘v’ , ‘a’};</p><p>String str = new String(ch);</p><p>This will initialise a new string that will have java as its content.</p><p>The above two lines can be written as</p><p>String str = ”java”;</p><p>Our first way of initialising a string showed us that strings contain an array of characters.</p><p>Java Strings are said to be immutable. Immutable means unmodifiable or unchangeable which means once a string object is created then we can not change its content.</p><p>Below we can see an example depicting this fact.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93850952adfe75f01571b6659ed10d98/href">https://medium.com/media/93850952adfe75f01571b6659ed10d98/href</a></iframe><p>Here Gaurav is not changed but a new object is created with Gaurav Sharma. That is why String is known as immutable.</p><p>But if we explicitly assign it to the reference variable, it will refer to the Gaurav Sharma object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51e5d844bea29b0bc9f1ce9ed9033140/href">https://medium.com/media/51e5d844bea29b0bc9f1ce9ed9033140/href</a></iframe><p>Here we changed the reference of variable s to string “Gaurav sharma” from “Gaurav”</p><h4>Operations with strings</h4><p>There are many operations that we can perform with strings I am going to list the most important here.</p><ol><li><strong>String concatenation: <br></strong>This section deals with joining two or more strings using different techniques</li></ol><ul><li><strong>String concatenation using + operator</strong></li></ul><p>We can join two Strings using the + operator and it’ll return a String which will have the contents of both of the strings appended to each other.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d57b2c6be220eda1dd9db941a9045322/href">https://medium.com/media/d57b2c6be220eda1dd9db941a9045322/href</a></iframe><p>But + operator can lead to ambiguity in the case of concatenating strings with numbers let&#39;s understand this point with an example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca5c6a474d749b14cfd5a6e6a02bbef1/href">https://medium.com/media/ca5c6a474d749b14cfd5a6e6a02bbef1/href</a></iframe><p>In this example, we can see that concatenation works strange enough to make us think again about the output</p><blockquote>Here we got the output <em>50TestString2040</em> because the <strong>string concatenation using + creates a new string and it keeps appending operands onto the end of the preceding operands.</strong></blockquote><p>Let’s understand this statement a little bit more by using step by step approach.</p><p>The statement10+20+20+s1+s2+20+40 works as follows.</p><p>10+20=30<br>30+20=50<br>50+s1=50Test<br>50Test+s2=50TestString<br>50TestString+20=50TestString20<br>50TestString20+40=50TestString2040</p><p><em>NOTE: An integer appended to the string will always result in a string.</em></p><ul><li><strong>String concatenation by concat method:</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51e5d844bea29b0bc9f1ce9ed9033140/href">https://medium.com/media/51e5d844bea29b0bc9f1ce9ed9033140/href</a></iframe><p><strong>2. Comparing Strings</strong></p><p>Various techniques are used to check the equality of strings in terms of values and the reference. There are 3 ways in java to compare string values.</p><ul><li><strong>String compare by equals method</strong></li></ul><p>The String equals method compares the strings based on their contents. It compares strings for parity or equality.</p><p>String class provides 2 methods for comparison of contents.</p><p>public Boolean equals(String str)<br>public Boolean equalsIgnoreCase(String str)</p><p>equals method compares two strings normally but it also takes account of the case too</p><p>“Apple” != “apple”</p><p>but in the case of equalsIgnoreCase the statement will be true</p><p>“Apple” == “apple”</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ea91178c9b866f6d02de89d977c128b/href">https://medium.com/media/5ea91178c9b866f6d02de89d977c128b/href</a></iframe><ul><li><strong>String comparison by == Operator</strong></li></ul><p>The == operator compares the reference or memory location of objects in a heap. It doesn’t check for the value it checks for reference.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc17b596d05413551857ae6f5fb6a751/href">https://medium.com/media/bc17b596d05413551857ae6f5fb6a751/href</a></iframe><ol><li>s1==s2 is true because both have the same addresses in the string constant pool. To know more about string constant pool please refer to this link because this topic is large and out of the scope of this tutorial.<br><a href="https://www.geeksforgeeks.org/string-constant-pool-in-java/">https://www.geeksforgeeks.org/string-constant-pool-in-java/</a></li><li>s1==s3 is false because whenever we are creating a string object with the new keyword we are allocating a new memory location in the string constant pool for this object so s1 and s3 don’t refer to the same memory location in the heap.</li></ol><ul><li><strong>String compare with compareTo() method</strong></li></ul><p>The compareTo() method compares both of the strings lexicographically and returns an integer value that indicates the first string is less than, equal or greater than the second string. For two strings s1 and s2 if</p><p>s1==s2 returns 0<br>s1&gt;s2 returns positive number<br>s1&lt;s2 returns negative number</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89d82c6785b1280c7f4ca5f50117c9dd/href">https://medium.com/media/89d82c6785b1280c7f4ca5f50117c9dd/href</a></iframe><p>Here we can see that the compareTo method checks only the relative ordering of letters in two strings.</p><h4>Some important methods of String class</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TajffgyRBwwVBi7cU50L6A.png" /></figure><h3>STRINGBUFFER CLASS</h3><p>The String and StringBuffer classes are defined in java.lang package. StringBuffer class is used to create a mutable or modifiable string. Everything is the same in String and StringBuffer except that the StringBuffer class represents a growable and writeable sequence of characters. It is possible to replace any character in the StringBuffer and we can also insert characters in the StringBuffer and the StringBuffer will grow in size by itself.</p><p>We can declare StringBuffer in 3 ways (it has 3 constructors) and we are going to see all of them.</p><ol><li>StringBuffer str = new StringBuffer(); <br>This will create a StringBuffer object which is an empty string buffer with an initial capacity of 16 characters.</li><li>StringBuffer str = new StringBuffer(String string);<br>This will create a StringBuffer object with the content of the string passed as the parameter.</li><li>StringBuffer str = new StringBuffer(int size); <br>It will create an empty StringBuffer object with an initial size equal to that integer passed as the parameter.</li></ol><h4>StringBuffer Methods:</h4><p>Some methods make Stringbuffers stand out from Strings and we are going to discuss them in detail</p><ul><li><strong>append():</strong></li></ul><p>The append method appends the given argument to the string. It takes 3 types of arguments String, int, and Object.</p><blockquote><strong><em>Syntax: <br>StringBuffer append(String str)<br>StringBuffer append(int num)<br>StringBuffer append(Object obj)</em></strong></blockquote><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c5234aa9a7f0fc8f053f417f1a29ee5/href">https://medium.com/media/9c5234aa9a7f0fc8f053f417f1a29ee5/href</a></iframe><ul><li><strong>insert()</strong></li></ul><p>The insert() method inserts the given string, character, or object into another string at the given position.</p><blockquote><strong><em>Syntax: <br>StringBuffer insert(int index, String str)<br>StringBuffer insert(int index, char ch)<br>StringBuffer insert(int index, Object obj)</em></strong></blockquote><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93ad6655fee025b4fd3a7ce3f426b0c0/href">https://medium.com/media/93ad6655fee025b4fd3a7ce3f426b0c0/href</a></iframe><ul><li><strong>delete()</strong></li></ul><p>The delete() method is used to delete a character from the string at an index or we can delete characters in a range.</p><blockquote><strong><em>Syntax: <br>StringBuffer insert(int startIndex, int endIndex)</em></strong></blockquote><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7022c5f2abee537cdb52ac4faaabd7c4/href">https://medium.com/media/7022c5f2abee537cdb52ac4faaabd7c4/href</a></iframe><ul><li><strong>reverse()</strong></li></ul><p>This method reverses the string. It does the in-place reversal of the string.</p><blockquote><strong><em>Syntax: <br>StringBuffer reverse()</em></strong></blockquote><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee7e5266f9d7a3f5cb80e2df8a133622/href">https://medium.com/media/ee7e5266f9d7a3f5cb80e2df8a133622/href</a></iframe><ul><li><strong>capacity()</strong></li></ul><p>The capacity() method of the StringBuffer class in Java is used to return buffer capacity. By default, a string buffer has a 16 character capacity. By calling this method, we can calculate its current capacity.</p><blockquote>If the number of characters is increased by the current capacity Stringbuffer increases its size by (current capacity*2)+2</blockquote><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aefc9291e39481fbb41fe20783abc40a/href">https://medium.com/media/aefc9291e39481fbb41fe20783abc40a/href</a></iframe><h3>STRINGBUILDER CLASS</h3><p>StringBuilder class is used to create mutable and modifiable strings same as the StringBuffer class but the only difference between StringBuffer and StringBuilder is that StringBuffer is synchronized. All the methods of StringBuffer are available in StringBuilder.</p><h4>DIFFERENCES BETWEEN STRING AND STRINGBUFFER CLASS</h4><p><strong>String</strong></p><ol><li>The String class is immutable.</li><li>String is slow and consumes more memory when we concatenate too many strings because every time it creates a new instance.</li><li>String class overrides the equals() method of the Object class. So you can compare the contents of two strings by the equals() method.</li><li>String class is slower while performing concatenation operations.</li><li>String class uses String constant pool.</li></ol><p><strong>StringBuffer</strong></p><ol><li>StringBuffer is fast and consumes less memory when we concatenate two strings.</li><li>StringBuffer class doesn’t override the equals() method of the Object class.</li><li>StringBuffer class is faster while performing concatenation operations.</li><li>The StringBuffer class is mutable.</li><li>StringBuffer uses Heap memory.</li></ol><h4>DIFFERENCES BETWEEN STRINGBUILDER AND STRINGBUFFER CLASS</h4><p><strong>StringBuffer</strong></p><ol><li>StringBuffer is <em>synchronized</em> i.e. thread-safe. It means two threads can’t call the methods of StringBuffer simultaneously.</li><li>StringBuffer is <em>less efficient</em> than StringBuilder.</li><li>StringBuffer was introduced in Java 1.0</li></ol><p><strong>StringBuilder</strong></p><ol><li>StringBuilder is <em>non-synchronized</em> i.e., not thread-safe. It means two threads can call the methods of StringBuilder simultaneously.</li><li>StringBuilder is <em>more efficient</em> than StringBuffer.</li><li>StringBuilder was introduced in Java 1.5</li></ol><p>So this is all about String, StringBuffer and StringBuilder classes here we learned what these classes are, what they can do, and what they cannot do. I hope you enjoyed reading and learning about these classes in deep.</p><p><em>If you understood anything I said here please give a clap reaction and follow me because it helps me to stay motivated and take out time from my schedule to write these articles. It will not cost you anything but will help me a lot</em></p><blockquote>Gaurav sharma is an avid reader and a passionate traveller. He is trying to live a more meaningful and purposeful life by spreading his knowledge and his life experiences! Follow him on this new journey of balancing digital and physical life. He is living in Uttarakhand, India. He is on Instagram at <a href="https://www.instagram.com/golf._.sierra/">@golf._.sierra</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=763b2e1f5fe6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/easy-tutorial-on-java-string-stringbuilder-and-stringbuffer-763b2e1f5fe6">Easy tutorial on Java String, StringBuilder and StringBuffer</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pure functions in JavaScript (demystified)]]></title>
            <link>https://golfsierra.medium.com/pure-functions-in-javascript-demystified-c43a3b96eefc?source=rss-19de845095af------2</link>
            <guid isPermaLink="false">https://medium.com/p/c43a3b96eefc</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Thu, 21 Apr 2022 10:46:06 GMT</pubDate>
            <atom:updated>2022-04-30T03:27:52.519Z</atom:updated>
            <content:encoded><![CDATA[<p>As a developer, we all have been writing code in our day-to-day life and writing code is not the only responsibility on our shoulders (Writing good code is).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gNzv-njMbLAarLtd" /><figcaption>Photo by <a href="https://unsplash.com/@enginakyurt?utm_source=medium&amp;utm_medium=referral">engin akyurt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In the daily coding sessions, you must be writing functions that take in some input and return some output based on certain conditions. While writing functions we should take care of certain conditions, We need to make sure these functions are,</p><ul><li>Predictable: It produces a predictable output for the same inputs.</li><li>Readable: Anyone reading the function as a standalone unit can understand its purpose completely.</li><li>Reusable: Can reuse the function at multiple places of the source code without altering its and the caller’s behavior.</li><li>Testable: We can test it as an independent unit.</li></ul><p>Now a pure function has all of the above characteristics. It’ll always produce the same output for the same input and it is a reusable piece of code. For example, let’s take a look at the piece of code below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17c78c0e0aad5798d296cddd1d39cfeb/href">https://medium.com/media/17c78c0e0aad5798d296cddd1d39cfeb/href</a></iframe><p>Here as we can see this function is always going to return the same output for the same input no matter what happens because it does not have any outside dependency that will make its output different.</p><p>Now let’s take a different scenario where the output can be different in different executions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/532130da703eea431d943043b34370ff/href">https://medium.com/media/532130da703eea431d943043b34370ff/href</a></iframe><p>Here we can see a function that looks almost the same as our older function but here is a catch the output of this function is not going to be the same for the same input (At least we can not guarantee this claim) because this function has a dependency and that dependency is greeting variable. imagine if we change the value of this variable tohello then the output is going to vary from what it is now.</p><p>So, here we have seen the side-effect of depending on an outer state value that may change without the function being aware of it.</p><p>Any operation that is not directly related to the final output of the function is called a Side Effect.</p><p>Now let’s take one more example of a non-pure function in practice</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afddfb840bca7f706d5671cd82a31e36/href">https://medium.com/media/afddfb840bca7f706d5671cd82a31e36/href</a></iframe><p>Can you tell me what will be the output of the function? Yes it’s predictable and it’ll always return the same output for the same input but it’s not a pure function because it is modifying a global variable. There is a set of things that a pure function can’t do</p><ol><li>A pure function can’t modify a global variable.</li><li>A pure function should not have its output dependent on an external value or a global variable.</li><li>A pure function can’t manipulate the DOM (because you are “mutating” something outside of the function scope.)</li><li>Logging(even in the console) becauseconsole is an <a href="https://developer.mozilla.org/en-US/docs/Web/API">external API</a>, not a JavaScript method.</li><li>Making an XHR/fetch call. because we are using an external source.</li></ol><p>So this is all about pure functions here we learned what pure functions are, what they can do, and what they should not do. I hope you enjoyed reading and learning about pure functions.</p><p>Here comes the final question So, Can I make all functions Pure Functions?</p><p>Yes, technically, you can. But the application with only pure functions may not do much. Your application program will have side effects like HTTP calls, IO operations, and many more. Please use pure functions in as many places as you find possible. Isolate impure functions(side effects) as much as possible. It will improve your program’s readability, debuggability, and testability a lot.</p><p><em>If you understood anything I said here please give a clap reaction and follow me because it helps me to stay motivated and take out time from my schedule to write these articles. It will not cost you anything but will help me a lot</em></p><blockquote>Gaurav sharma is an avid reader and a passionate traveller. He is trying to live a more meaningful and purposeful life by spreading his knowledge and his life experiences! Follow him on this new journey of balancing digital and physical life. He is living in Uttarakhand, India. He is on Instagram at <a href="https://www.instagram.com/golf._.sierra/">@golf._.sierra</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c43a3b96eefc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>