<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Syket Bhattachergee</title>
    <description>The latest articles on DEV Community by Syket Bhattachergee (@syketb).</description>
    <link>https://dev.to/syketb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1655033%2Ff2db59f2-2f86-4549-bcd3-bb53c71d0a49.jpg</url>
      <title>DEV Community: Syket Bhattachergee</title>
      <link>https://dev.to/syketb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/syketb"/>
    <language>en</language>
    <item>
      <title>Understanding Big O Notation: A Beginner's Guide to Time and Space Complexity</title>
      <dc:creator>Syket Bhattachergee</dc:creator>
      <pubDate>Thu, 24 Oct 2024 06:25:36 +0000</pubDate>
      <link>https://dev.to/creowistech/understanding-big-o-notation-a-beginners-guide-to-time-and-space-complexity-4a27</link>
      <guid>https://dev.to/creowistech/understanding-big-o-notation-a-beginners-guide-to-time-and-space-complexity-4a27</guid>
      <description>&lt;p&gt;Imagine we have different ways to write the same code. How do we figure out which one is the best? That's where Big O notation comes in. It helps us measure how much time and space the code will need, making it easier to compare them.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Big-O Notation?
&lt;/h3&gt;

&lt;p&gt;Big O, also known as "Order of," is a way to describe how long an algorithm might take to run in the worst-case scenario. It gives us an idea of the maximum time an algorithm will need based on the input size. We write it as O(f(n)), where f(n) shows how many steps the algorithm takes to solve a problem with input size n.&lt;/p&gt;

&lt;p&gt;Let’s try to understand by an example &lt;em&gt;“Write a function that accepts a string input and returns a reversed copy“&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhav0qudbd9mksdn4hr4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhav0qudbd9mksdn4hr4f.png" alt="Solution for writing a function that accepts a string as input and returns a reversed copy." width="800" height="2139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, I'm sharing a Stack Overflow solution with you, along with the image above, as you can see. It shows 10 different ways to solve the same problem, each with a unique approach.&lt;/p&gt;

&lt;p&gt;Now, the question is: how do we know which one is the best?&lt;/p&gt;

&lt;p&gt;Our goal is to understand time and space complexity. To do this, let's explore a concrete example with two different code snippets, so we can clearly see how these concepts matter.&lt;/p&gt;

&lt;p&gt;Here's a function called &lt;code&gt;addUpTo&lt;/code&gt;. This function uses a &lt;code&gt;for&lt;/code&gt; loop that runs until it reaches the length of &lt;code&gt;n&lt;/code&gt; and returns the total sum.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgw9r5srttgmgb28fj8r9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgw9r5srttgmgb28fj8r9.png" alt="A function uses a for loop that runs until it reaches the length of n and returns the total sum." width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's look at another example that achieves the same functionality:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yd7si3dg38ar1fgvbda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yd7si3dg38ar1fgvbda.png" alt="A function uses a formula to find the total sum of n." width="800" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Which one is better?
&lt;/h3&gt;

&lt;p&gt;Now, what does 'better' really mean in this context?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Is it faster?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Does it use less memory?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is it more readable?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typically, people focus on the first two—speed and memory usage—before considering readability. In this case, we'll be focusing on the first two. To measure the performance of both code examples, we'll use JavaScript's built-in timing functions to analyze and compare their execution times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo0vg0kxeau7zk5aelgn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo0vg0kxeau7zk5aelgn.png" alt="Measuring time using the performance.now() function in JavaScript." width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code above, I've added &lt;code&gt;t1&lt;/code&gt; and &lt;code&gt;t2&lt;/code&gt;, which utilizes the &lt;a href="http://performance.now" rel="noopener noreferrer"&gt;&lt;code&gt;performance.now&lt;/code&gt;&lt;/a&gt;&lt;code&gt;()&lt;/code&gt; function built into JavaScript. This function helps us measure the time taken by the code to execute, allowing us to accurately compare the performance of the two approaches.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7hujkvc3limxh3uanbz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7hujkvc3limxh3uanbz.png" alt="Output of the function with time calculation" width="800" height="106"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Exactly, the execution time varies depending on different factors, and you can see that on my computer as well—the times aren't consistent. The &lt;a href="http://performance.now" rel="noopener noreferrer"&gt;&lt;code&gt;performance.now&lt;/code&gt;&lt;/a&gt;&lt;code&gt;()&lt;/code&gt; function doesn't give us a fixed time, but it provides a useful estimate for comparison.&lt;/p&gt;

&lt;p&gt;Now, let's take a look at the second example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35zfmbf6smx79xygxbcr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35zfmbf6smx79xygxbcr.png" alt="Measuring time using the performance.now() function in JavaScript." width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8h4jcy5avq0goyutx80s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8h4jcy5avq0goyutx80s.png" alt="Output of the function with time calculation" width="800" height="79"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, observe the difference in execution time between the first and second code examples. The second one is noticeably faster than the first.&lt;/p&gt;

&lt;p&gt;But what's the issue with relying solely on time measurements?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Different machines record different times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even the same machine will record different times on each run.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For very fast algorithms, speed measurements may not be precise enough to give an accurate comparison.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These factors make time-based performance assessments unreliable, especially for quick algorithms.&lt;/p&gt;

&lt;p&gt;Okay, if not time then what?&lt;/p&gt;

&lt;p&gt;That’s where Big O will help us to measure the time complexity and space both. So Big O Notation is a way to formalize fuzzy counting. It allows us to talk formally about how the runtime of algorithm grows as the input grows.&lt;/p&gt;

&lt;p&gt;We say that an algorithm is O(f(n)) if the number of simple operations the computer has to do is eventually less than a constant times f(n), as n increases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;f(n) could be linear (f(n) = n)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;f(n) could be quadratic (f(n) = n2)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;f(n) could be constant (f(n) = 1)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;f(n) could be something entirely different!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yd7si3dg38ar1fgvbda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0yd7si3dg38ar1fgvbda.png" alt="Calculating time complexity of the addUpTo function" width="800" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, we only have 3 operations, and no matter the size of the input, it will always remain 3 operations. This is why we can say that the time complexity of this function is constant, or &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now here is the first example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgw9r5srttgmgb28fj8r9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgw9r5srttgmgb28fj8r9.png" alt="Calculating the time complexity of the addUpTo function." width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, as the input &lt;code&gt;n&lt;/code&gt; grows, the number of operations also increases proportionally. Therefore, the time complexity depends on the size of the input, making it &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's look at another example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32outg96oqdjs55e8z1u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32outg96oqdjs55e8z1u.png" alt="Calculating the time complexity of the printAllPairs function." width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we have two nested &lt;code&gt;for&lt;/code&gt; loops, both dependent on &lt;code&gt;n&lt;/code&gt;—the input. This means that as the input &lt;code&gt;n&lt;/code&gt; grows, the number of operations increases significantly. As a result, the time complexity of this code is &lt;strong&gt;O(n²)&lt;/strong&gt;, also known as quadratic time complexity.&lt;/p&gt;

&lt;p&gt;Now, let's simplify &lt;strong&gt;Big O Notation&lt;/strong&gt;. When determining the time complexity of an algorithm, there are a few helpful rules of thumb to keep in mind. These guidelines arise from the formal definition of Big O notation and make it easier to analyze algorithms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constants don’t matter
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;O(2n) = O(n)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(500) = O(1)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(13n²) = O(n²)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(n+ 10) = O(n)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(1000n + 500) = O(n)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;O(n² + 5n + 8) = O(n²)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Big O Shorthands
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Arithmetic operations are constant&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variable assignment is constant&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessing elements in an array (by index) or object (by key) is constant&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In a loop, the complexity is the length of the loop times the complexity of whatever happens inside the loop&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is the Big O Chart:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhpqxev5mepbnc7utcw7c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhpqxev5mepbnc7utcw7c.png" alt="Chart of complexities in Big O." width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far, from the examples, we've learned about &lt;strong&gt;O(n)&lt;/strong&gt; (linear time complexity), &lt;strong&gt;O(1)&lt;/strong&gt; (constant time complexity), and &lt;strong&gt;O(n²)&lt;/strong&gt; (quadratic time complexity). These cover the basic patterns where operations grow with input size.&lt;/p&gt;

&lt;p&gt;We'll discuss &lt;strong&gt;O(log n)&lt;/strong&gt; and &lt;strong&gt;O(n log n)&lt;/strong&gt;—logarithmic and linearithmic time complexities—a bit later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Space Complexity
&lt;/h3&gt;

&lt;p&gt;So far, we've been focusing on time complexity: how can we analyze the runtime of an algorithm as the size of the inputs increases?&lt;/p&gt;

&lt;p&gt;We can also use big O notation to analyze space complexity: how much additional memory do we need to allocate in order to run the code in our algorithm?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about the inputs?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes you'll hear the term auxiliary space complexity to refer to space required by the algorithm, not including space taken up by the inputs. Unless otherwise noted, when we talk about space complexity, technically we'll be talking about auxiliary space complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity in JS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Most primitives (booleans, numbers, undefined, null) are constant space&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Strings require O(n) space (where n is the string length) Reference types are generally O(n), where n is the length (for arrays) or the number of keys (for objects)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9odie70gmzmu1dm6mjq5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9odie70gmzmu1dm6mjq5.png" alt="Space complexity of the sum function." width="800" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember, we're talking about &lt;strong&gt;space complexity&lt;/strong&gt;, not time complexity. In this code, we can see we only have two variables. No matter how large the input grows, these two variables will always exist in the code. Since we're not adding any new variables as the input increases, we can say that the space complexity is &lt;strong&gt;O(1)&lt;/strong&gt;, meaning it's constant space complexity.&lt;/p&gt;

&lt;p&gt;Let’s understand with another example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wn6h13ettfaz6yju5q8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wn6h13ettfaz6yju5q8.png" alt="Space complexity of the double function." width="800" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this code, the function returns &lt;code&gt;newArr&lt;/code&gt; after doubling every element in the input array. Since the size of &lt;code&gt;newArr&lt;/code&gt; is directly proportional to the size of the input &lt;code&gt;arr&lt;/code&gt;, the space used by &lt;code&gt;newArr&lt;/code&gt; grows with the input size. Unlike the previous example, where space was constant, here the space complexity depends on the size of the input array. Therefore, the space complexity is &lt;strong&gt;O(n)&lt;/strong&gt;, meaning it's linear.&lt;/p&gt;

&lt;p&gt;I hope this makes space complexity clearer—just like time complexity, we're measuring it using Big O notation. So far, you've learned about different time complexities, in increasing order of complexity: &lt;strong&gt;O(1)&lt;/strong&gt;, &lt;strong&gt;O(log n)&lt;/strong&gt;, &lt;strong&gt;O(n)&lt;/strong&gt;, &lt;strong&gt;O(n log n)&lt;/strong&gt;, &lt;strong&gt;O(n²)&lt;/strong&gt;, &lt;strong&gt;O(n³)&lt;/strong&gt;, and so on. However, we haven't yet explored logarithmic time complexity.&lt;/p&gt;

&lt;p&gt;Next, we'll dive into &lt;strong&gt;logarithmic time complexity&lt;/strong&gt; and see how it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logarithms
&lt;/h3&gt;

&lt;p&gt;We've encountered some of the most common complexities: O(1), O(n), O(n2). Sometimes big O expressions involve more complex mathematical expressions. One that appears more often than you might like is the logarithm!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's a log again?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;log2 (8) = 3 → 2³ = 8&lt;/p&gt;

&lt;p&gt;log2 (value) = exponent → 2^exponent = value&lt;/p&gt;

&lt;p&gt;We'll omit the 2, which means:&lt;/p&gt;

&lt;p&gt;log === log2&lt;/p&gt;

&lt;p&gt;The logarithm of a number roughly measures how many times you can divide that number by 2 before it becomes less than or equal to 1. This makes &lt;strong&gt;logarithmic time complexity&lt;/strong&gt; very efficient. As you saw in the chart, &lt;strong&gt;O(1)&lt;/strong&gt; (constant time) is very close to &lt;strong&gt;O(log n)&lt;/strong&gt;, which is fantastic! It's also worth noting that &lt;strong&gt;O(n log n)&lt;/strong&gt; is much better than &lt;strong&gt;O(n²)&lt;/strong&gt; in terms of efficiency, making it a preferred complexity for many algorithms.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Certain searching algorithms have logarithmic time complexity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Efficient sorting algorithms involve logarithms.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recursion sometimes involves logarithmic space complexity.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Great! We've covered most of the key points on space and time complexity. Keep in mind that mastering these concepts takes practice, so don't feel overwhelmed if it's not completely clear right away. Take your time, review the material multiple times, and revisit examples as needed. It often takes a few repetitions for these ideas to really sink in, so be patient with yourself!&lt;/p&gt;

&lt;p&gt;Finally, let’s do one more time recap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To analyze the performance of an algorithm, we use Big O Notation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Big O Notation can give us a high-level understanding of the time or space complexity of an algorithm&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Big O Notation doesn't care about precision, only about general trends (linear? quadratic? constant?)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Again, Big O Notation is everywhere, so get lots of practice!&lt;/p&gt;




&lt;p&gt;We at &lt;a href="https://creowis.com/" rel="noopener noreferrer"&gt;&lt;strong&gt;CreoWis&lt;/strong&gt;&lt;/a&gt; believe in sharing knowledge publicly to help the developer community grow. Let’s collaborate, ideate, and craft passion to deliver awe-inspiring product experiences to the world.&lt;/p&gt;

&lt;p&gt;Let's connect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://twitter.com/creowistech" rel="noopener noreferrer"&gt;X/Twitter&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.linkedin.com/company/creowis/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://creowis.com/" rel="noopener noreferrer"&gt;Website&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is crafted by &lt;strong&gt;Syket Bhattachergee&lt;/strong&gt;, a passionate developer at CreoWis. You can reach out to him on &lt;a href="https://twitter.com/syketb_twt" rel="noopener noreferrer"&gt;&lt;strong&gt;X/Twitter&lt;/strong&gt;&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/syketb" rel="noopener noreferrer"&gt;&lt;strong&gt;LinkedIn&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;,&lt;/strong&gt; and follow his work on the &lt;a href="https://github.com/syket-git" rel="noopener noreferrer"&gt;&lt;strong&gt;GitHub&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Magical Redis Cache: A Website's Best Friend</title>
      <dc:creator>Syket Bhattachergee</dc:creator>
      <pubDate>Thu, 25 Jul 2024 11:36:38 +0000</pubDate>
      <link>https://dev.to/syketb/the-magical-redis-cache-a-websites-best-friend-5340</link>
      <guid>https://dev.to/syketb/the-magical-redis-cache-a-websites-best-friend-5340</guid>
      <description>&lt;p&gt;Imagine you're running a busy coffee shop. Customers come in, place their orders, and wait for their drinks. At first, you're making each drink from scratch every time. It's slow, and people are getting impatient.&lt;/p&gt;

&lt;p&gt;Then one day, you have a brilliant idea. What if you could make the most popular drinks ahead of time and keep them ready? That way, when someone orders a regular latte, you can just grab it and go. Suddenly, your service is lightning fast!&lt;/p&gt;

&lt;p&gt;This is exactly what Redis does for websites, but instead of coffee, it's dealing with data.&lt;/p&gt;

&lt;p&gt;Let's say you have a website that shows the weather. Every time someone visits, your website has to ask a weather service for the latest information. It's like making a fresh coffee each time - slow and inefficient.&lt;/p&gt;

&lt;p&gt;Enter Redis, the magical helper. Redis is like a super-fast notepad that remembers things for your website. When the first person asks for the weather, Redis writes it down. When the next person asks, instead of going back to the weather service, your website just checks Redis's notepad. Boom! The answer is there, ready to go.&lt;/p&gt;

&lt;p&gt;But Redis isn't just a one-trick pony. Oh no, it's got more tricks up its sleeve:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;"The Forgetful Notepad": Redis can automatically erase old information after a set time. So your weather data doesn't get stale.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"The Countmaster": Need to know how many people visited your site today? Redis can keep count for you, faster than you can say "one, two, three."&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"The Queue Manager": Imagine your website needs to send out a thousand emails. Redis can create a neat queue, so your website doesn't get overwhelmed trying to do everything at once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"The Matchmaker": Redis is great at quickly finding connections between different pieces of information. This is super useful for things like social media sites that need to show you posts from your friends.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, the next time you visit a website and it loads in the blink of an eye, give a little nod to Redis. It might just be working its magic behind the scenes, serving up data faster than a barista with a pot of pre-made coffee.&lt;/p&gt;

&lt;p&gt;Remember, in the world of websites, every millisecond counts. And Redis? It's the speedster that helps keep the internet running smooth and fast, one piece of data at a time.&lt;/p&gt;

&lt;p&gt;I am Syket Bhattachergee, Software Engineer at CreoWis and Content Creator at &lt;a href="https://youtube.com/@syketb?si=wfID7P8hUsCvYs_J" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;. If you want to discuss your technical writing needs or any role? You can reach out to me on &lt;a href="https://linkedin.com/in/syketb" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and follow my work on &lt;a href="https://github.com/syket-git" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>website</category>
      <category>node</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Ultimate Guide to React Native Image Components</title>
      <dc:creator>Syket Bhattachergee</dc:creator>
      <pubDate>Wed, 03 Jul 2024 12:06:04 +0000</pubDate>
      <link>https://dev.to/syketb/ultimate-guide-to-react-native-image-components-4noa</link>
      <guid>https://dev.to/syketb/ultimate-guide-to-react-native-image-components-4noa</guid>
      <description>&lt;p&gt;Have you ever tried to work with images in your React Native app and felt a bit lost? Don't worry, you're not alone! Handling images in React Native can seem a little different from what you're used to on the web, but once you understand the basics, it becomes a breeze.&lt;/p&gt;

&lt;p&gt;Let's start with the Image component. React Native provides this built-in component to display images in your app, just like how you'd use a &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; tag on the web. However, there's a catch: if you want to set an image as a background, you'll need to use a different component called ImageBackground.&lt;/p&gt;

&lt;p&gt;Now, you might be thinking, "Why can't I just use the Image component for backgrounds too?" Well, the Image component is designed specifically for displaying standalone images, like profile pictures or product images. It's optimized for performance and comes with features like caching and resizing out of the box.&lt;/p&gt;

&lt;p&gt;On the other hand, the  component is meant for, you guessed it, setting images as backgrounds for your app's views or components. This component acts as a container that stretches the image to fit its entire area, making it perfect for creating visually appealing backgrounds.&lt;/p&gt;

&lt;p&gt;Today, our goal is to break down the Image component and explore how we can use it to meet our various requirements.&lt;/p&gt;

&lt;p&gt;As I mentioned, the Image component is provided by React Native, so it's necessary to import the Image component from React Native.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Amazing! Now, we will show three different examples of using the Image component in React Native with various variations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;StatusBar&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;expo-status-bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;SafeAreaView&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-native-safe-area-context&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SafeAreaView&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;flex-1 p-4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;flex-1 space-y-3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt;
          &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;w-28 h-28&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="nx"&gt;resizeMethod&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;contain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../assets/images/image.jpg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
        &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt;
          &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;w-28 h-28 rounded-full&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;
            &lt;span class="na"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://letsenhance.io/static/8f5e523ee6b2479e26ecc91b9c25261e/1015f/MainAfter.jpg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;}}&lt;/span&gt;
        &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StatusBar&lt;/span&gt; &lt;span class="nx"&gt;backgroundColor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;auto&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/SafeAreaView&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here is the output of the above code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxcmj42joys4w1gke1xa.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxcmj42joys4w1gke1xa.jpg" alt="React Native Image Component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above component, you will notice two images. My concern is not with the images themselves but with the use of require() and URI. If you look closely, you'll see we used two different source options depending on the URL. For plain image URLs, you have to use URI instead of require(). If your images are already in your project directory in a static location, you can use the require() method.&lt;/p&gt;

&lt;p&gt;I hope you understand how the Image component works in different setups. Depending on the situation, we can use either require() or a URI.&lt;/p&gt;

&lt;p&gt;So far, we have covered the usage of the Image component. However, this is not the end of the story for the Image component, there are a couple of important things to discuss.&lt;/p&gt;

&lt;p&gt;When it comes to the props of the Image component, the list is extensive. I highly recommend looking into the React Native Image component documentation. There, you will find numerous props that we can pass into the Image component, which can simplify our work based on your requirements.&lt;/p&gt;

&lt;p&gt;For example, if we want to use the alt prop in the Image component, here is the code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt;
   &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;w-28 h-28&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
   &lt;span class="nx"&gt;resizeMethod&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;contain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
   &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../assets/images/image.jpg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
   &lt;span class="nx"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wildlife image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It's one example of how you can leverage the prop in the Image component.&lt;/p&gt;

&lt;p&gt;Well, we have covered almost all of the important points. Just one more thing I want to highlight is the usage of GIF and WebP support on Android.&lt;/p&gt;

&lt;p&gt;When building your native code, GIF and WebP are not supported by default on Android. You will need to add some optional modules in android/app/build.gradle, depending on the needs of your app.&lt;/p&gt;

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

dependencies {
  // If your app supports Android versions before Ice Cream Sandwich (API level 14)
  implementation 'com.facebook.fresco:animated-base-support:1.3.0'

  // For animated GIF support
  implementation 'com.facebook.fresco:animated-gif:3.1.3'

  // For WebP support, including animated WebP
  implementation 'com.facebook.fresco:animated-webp:3.1.3'
  implementation 'com.facebook.fresco:webpsupport:3.1.3'

  // For WebP support, without animations
  implementation 'com.facebook.fresco:webpsupport:3.1.3'
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Amazing, I believe if you covered this part of the article, you won't have any problem using the Image component in your React Native applications. By thoroughly understanding the concepts and techniques discussed here, you'll be well-equipped to seamlessly integrate images into your projects, elevating the overall user experience and making your app more visually appealing.&lt;/p&gt;

&lt;p&gt;I am Syket Bhattachergee, Software Engineer at CreoWis. If you want to discuss your technical writing needs or any role? You can reach out to me on &lt;a href="https://linkedin.com/in/syketb" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and follow my work on &lt;a href="https://github.com/syket-git" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>reactnative</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to add NativeWind in React Native Expo</title>
      <dc:creator>Syket Bhattachergee</dc:creator>
      <pubDate>Mon, 01 Jul 2024 03:46:25 +0000</pubDate>
      <link>https://dev.to/syketb/how-to-add-nativewind-in-react-native-expo-3h55</link>
      <guid>https://dev.to/syketb/how-to-add-nativewind-in-react-native-expo-3h55</guid>
      <description>&lt;p&gt;Are you a frontend developer who's fallen in love with the simplicity and power of Tailwind CSS? If so, you're not alone! I, too, was enamored by Tailwind's utility-first approach and the way it streamlined my workflow. However, when I made the transition to React Native, I found myself missing the convenience of Tailwind's classes terribly.&lt;/p&gt;

&lt;p&gt;Writing CSS manually felt like a step back in time, and my productivity took a hit. That's when I found NativeWind, a game-changer for React Native developers who crave the same level of flexibility and ease that Tailwind offers.&lt;/p&gt;

&lt;p&gt;NativeWind is a utility library that brings the magic of Tailwind CSS to your React Native projects, allowing you to style your components with the same familiar classes you know and love. Say goodbye to the tedious task of writing CSS from scratch and hello to a world of rapid development and consistent styling.&lt;/p&gt;

&lt;p&gt;In this article, we'll dive deep into the world of NativeWind and explore how to integrate it into your React Native Expo project. Prepare to be amazed as you witness the seamless fusion of Tailwind's utility-first approach with the power of React Native.&lt;/p&gt;

&lt;p&gt;So, I'm assuming you have already created an Expo app. Now, our mission is to make our app capable of writing Tailwind CSS code.&lt;/p&gt;

&lt;p&gt;Add these two dependencies to your Expo project:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;nativewind&lt;/span&gt;
&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;dev&lt;/span&gt; &lt;span class="nx"&gt;tailwindcss&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;3.2&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, run npx tailwindcss init to create a tailwind.config.js file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// tailwind.config.js&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./App.{js,jsx,ts,tsx}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./&amp;lt;custom directory&amp;gt;/**/*.{js,jsx,ts,tsx}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;Modify&lt;/span&gt; &lt;span class="nx"&gt;your&lt;/span&gt; &lt;span class="nx"&gt;babel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// babel.config.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;babel-preset-expo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nativewind/babel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Believe it or not, that's it 🎉! Now your React Native app is eligible to use Tailwind CSS classes, just like your frontend app. Isn't that cool?&lt;/p&gt;

&lt;p&gt;Modify your App.js like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;StatusBar&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;expo-status-bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;flex-1 items-center justify-center bg-gray-600&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text-yellow-200 text-3xl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hey&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StatusBar&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run your app, and you will see the effects of the Tailwind CSS styles in your app as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sk60eh7imoia788imrl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sk60eh7imoia788imrl.jpg" alt="React Native App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's great! In this article, you learned a little bit about NativeWind, what it is, how it helps us in React Native, and how to integrate it with an Expo app.&lt;/p&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I am Syket Bhattachergee, Software Engineer at CreoWis. If you want to discuss your technical writing needs or any role? You can reach out to me on &lt;a href="https://linkedin.com/in/syketb" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://syketb.vercel.app" rel="noopener noreferrer"&gt;My Website&lt;/a&gt;, and follow my work on &lt;a href="https://github.com/syket-git" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>reactnative</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Error handling in Next.js App Router</title>
      <dc:creator>Syket Bhattachergee</dc:creator>
      <pubDate>Sat, 29 Jun 2024 08:40:29 +0000</pubDate>
      <link>https://dev.to/syketb/error-handling-in-nextjs-app-router-5b8a</link>
      <guid>https://dev.to/syketb/error-handling-in-nextjs-app-router-5b8a</guid>
      <description>&lt;p&gt;If you've been working with Next.js 14 and you've spotted the error.js file in your directory, you might be wondering what it's all about. Well, you're in luck! Today, we're going to break down this file and see how it helps us out.&lt;/p&gt;

&lt;p&gt;First of all, let's understand how the error file works. The error.js file convention allows you to gracefully handle unexpected runtime errors in nested routes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatically protect each part of your route along with its nested parts using a React Error Boundary.&lt;/li&gt;
&lt;li&gt;Design error displays are customized for each section by organizing files in a hierarchy.&lt;/li&gt;
&lt;li&gt;Contain errors within specific sections while keeping the rest of your app running smoothly.&lt;/li&gt;
&lt;li&gt;Implement features to try fixing errors without reloading the entire page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the most important things is that the error.js file should be a client-side component. There is no option to make it a server-side component, as specified in the Next.js documentation. Let's look at the code of the error.js file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Error components must be Client Components&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nl"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Log the error to an error reporting service&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt; &lt;span class="nx"&gt;went&lt;/span&gt; &lt;span class="nx"&gt;wrong&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;
        &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// Attempt to recover by trying to re-render the segment&lt;/span&gt;
          &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Try&lt;/span&gt; &lt;span class="nx"&gt;again&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here is an overview of how error.js file work:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmmgt3rc4phy72v01i1gl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmmgt3rc4phy72v01i1gl.jpg" alt="How error.js file work in next.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;error.js creates a React Error Boundary around a specific component or page.js. It uses the exported React component from error.js as a backup. If an error occurs within this boundary, it's isolated, and the backup component is shown. Despite the error, layouts above the boundary stay functional, allowing for error recovery options within the backup component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recovering From Errors
&lt;/h2&gt;

&lt;p&gt;If an error is temporary, retrying might fix it. Using the reset() function in an error component prompts users to try again. When reset() the Error Boundary attempts to re-render its contents. If successful, the fallback error component is replaced with the new result.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="nl"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
  &lt;span class="nl"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;br&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt; &lt;span class="nx"&gt;went&lt;/span&gt; &lt;span class="nx"&gt;wrong&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Try&lt;/span&gt; &lt;span class="nx"&gt;again&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Nested Routes&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Nested Routes in React involve rendering components in a structured hierarchy.&lt;/p&gt;

&lt;p&gt;For instance, consider a scenario where two nested segments each contain layout.js and error.js files. This creates a hierarchy like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nested Error Component Hierarchy&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Errors propagate upwards to the nearest parent error boundary. This means that an error.js file will manage errors for all its nested child segments.&lt;/li&gt;
&lt;li&gt;Adjustments to the granularity of error UI are possible by positioning error.js files at various levels within the nested route folders.&lt;/li&gt;
&lt;li&gt;However, an error boundary defined in an error.js file won't handle errors from a layout.js component in the same segment, as the error boundary is nested within that layout's component.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Handling Errors in Layouts
&lt;/h2&gt;

&lt;p&gt;Error boundaries defined error.js do not capture errors occurring in layout.js or template.js components within the same segment. This hierarchy is deliberate, ensuring that crucial UI elements shared among sibling routes, like navigation, remain accessible and usable during errors.&lt;/p&gt;

&lt;p&gt;To manage errors within a particular layout or template, include an error.js file in the parent segment of that layout.&lt;/p&gt;

&lt;p&gt;For handling errors within the root layout or template, utilize a variant named global-error.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When dealing with errors in root layouts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The root app/error.js boundary doesn't handle errors from root app/layout.js or app/template.js components.&lt;/li&gt;
&lt;li&gt;For handling errors in these root components, utilize a variant named app/global-error.js, placed in the root app directory.&lt;/li&gt;
&lt;li&gt;Unlike app/error.js, global-error.js wraps the entire application, replacing the root layout with its fallback component when active. Thus, global-error.js must include its own  and  tags.&lt;/li&gt;
&lt;li&gt;global-error.js serves as broad error handling for the entire application, less frequently triggered due to the static nature of root components. However, it's still advisable to have a root error.js file defining a fallback component within the root layout for consistent UI and branding.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="c1"&gt;// app/global-error.tsx&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;GlobalError&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;br&gt;
    &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;br&gt;
    &lt;span class="nx"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="nl"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
    &lt;span class="nl"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;br&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt; &lt;span class="nx"&gt;went&lt;/span&gt; &lt;span class="nx"&gt;wrong&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Try&lt;/span&gt; &lt;span class="nx"&gt;again&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/body&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/html&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;br&gt;
&lt;/span&gt;    &lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Handling Server Errors&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;When a Server Component encounters an error, Next.js forwards an Error object (with sensitive details removed in production) to the nearest error.js file as the error prop.&lt;/p&gt;

&lt;p&gt;To protect sensitive data, in production, only a generic message and a digest property are sent to the client. The digest is a hash of the error useful for matching with server-side logs.&lt;/p&gt;

&lt;p&gt;In development, the Error object sent to the client includes the original error message for easier debugging.&lt;/p&gt;

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

&lt;p&gt;In conclusion, error files, such as error.js or global-error.js, serve as crucial components in React applications for defining error UI boundaries within route segments. They effectively catch unexpected errors, whether originating from Server Components or Client Components and display fallback UI to maintain a smooth user experience. These files offer functionalities like resetting error boundaries and ensuring error handling consistency throughout the application. Additionally, the distinction between error.js and global-error.js provides flexibility in managing errors at different levels, from specific layouts to the root application. By adhering to best practices and leveraging tools like React Developer Tools, developers can optimize error-handling mechanisms to enhance the reliability and usability of their React applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I am Syket Bhattachergee, Software Engineer at CreoWis and Content Creator at &lt;a href="https://youtube.com/@syketb?si=wfID7P8hUsCvYs_J" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;. If you want to discuss your technical writing needs or any role? You can reach out to me on &lt;a href="https://linkedin.com/in/syketb" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and follow my work on &lt;a href="https://github.com/syket-git" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to create a Next.js 14 dynamic sitemap?</title>
      <dc:creator>Syket Bhattachergee</dc:creator>
      <pubDate>Fri, 21 Jun 2024 04:27:13 +0000</pubDate>
      <link>https://dev.to/syketb/how-to-create-a-nextjs-14-dynamic-sitemap-2e1b</link>
      <guid>https://dev.to/syketb/how-to-create-a-nextjs-14-dynamic-sitemap-2e1b</guid>
      <description>&lt;h2&gt;
  
  
  What is a sitemap?
&lt;/h2&gt;

&lt;p&gt;A sitemap is a file that contains a comprehensive list of all the pages on a website, along with their interrelationships. It plays a crucial role in aiding search engines to efficiently crawl and comprehend the structure of the website. Sitemaps also offer vital information about each page, such as its last update and frequency of changes. Particularly for larger or intricately structured websites, having a sitemap simplifies the process for search engines to discover and index all pages. According to Google, sitemaps prove beneficial for new websites with limited external links and those with content that lacks sufficient interlinking. In any scenario, integrating a sitemap can enhance a website's SEO by furnishing valuable insights to search engines and improving overall site navigation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is it necessary for my website?
&lt;/h2&gt;

&lt;p&gt;Imagine your blog as a big library, and a sitemap as a well-organized catalog that helps people find books easily. Now, think of this catalog as not just static but magically updating itself whenever a new book arrives or an old one gets a makeover. That's what a dynamic sitemap does for your blog in the vast world of the internet.&lt;/p&gt;

&lt;p&gt;Let's say your blog is built using Next.js 14 – a cool tool to create and manage your online library. The blog posts are like the books on your shelves, and the sitemap is your digital librarian, making sure search engines (like Google) and visitors can easily find and explore your literary treasures.&lt;/p&gt;

&lt;p&gt;Now, the traditional way would be to manually update this catalog every time you add or change a book – quite a tedious job! But with a dynamic sitemap, it's like having a magical librarian who does this for you in real time. Every new blog post you publish or update is instantly added to the catalog, making it a breeze for search engines to know about your latest content and for readers to discover it.&lt;/p&gt;

&lt;p&gt;Setting up your Next.js 14 project is like building the shelves and organizing the initial catalog. Once that's done, you teach your digital librarian to not only list the fixed sections but also dynamically add the latest books. This is where the coding magic happens.&lt;/p&gt;

&lt;p&gt;If you publish a new blog post, your sitemap automatically updates to include it. No need to manually tell the librarian about each new book – it's taken care of. This ensures that your library (blog) is always up-to-date, easily found by search engines, and provides a smooth reading experience for your visitors.&lt;/p&gt;

&lt;p&gt;For instance, create a "sitemap.js" file in your app directory for dynamic generation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { getAllPosts } from "./queries";

const sitemap = async () =&amp;gt; {
  const BASE_URL = "https://www.YOUR_DOMAIN.com/";

  const posts = await getAllPosts();

  const postURLS =
    posts?.length &amp;gt; 0
      ? (posts[1] || []).map((post) =&amp;gt; ({
          url: `${BASE_URL}/blog/${post?.slug}`,
          lastModified: new Date(post?.publishedAt),
        }))
      : [];

  return [
    {
      url: BASE_URL,
      lastModified: new Date(),
    },
    {
      url: `${BASE_URL}/blog`,
      lastModified: new Date(),
    },
    ...postURLS,
  ];
};
export default sitemap;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're done! Now you know how to generate it dynamically.&lt;/p&gt;

&lt;p&gt;I am Syket Bhattachergee, Software Engineer at CreoWis and Content Creator at &lt;a href="https://youtube.com/@syketb?si=wfID7P8hUsCvYs_J" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;. If you want to discuss your technical writing needs or any role? You can reach out to me on &lt;a href="https://linkedin.com/in/syketb" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and follow my work on &lt;a href="https://github.com/syket-git" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>seo</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to reduce unused JavaScript in your code?</title>
      <dc:creator>Syket Bhattachergee</dc:creator>
      <pubDate>Thu, 20 Jun 2024 06:00:37 +0000</pubDate>
      <link>https://dev.to/syketb/how-to-reduce-unused-javascript-in-your-code-mh7</link>
      <guid>https://dev.to/syketb/how-to-reduce-unused-javascript-in-your-code-mh7</guid>
      <description>&lt;p&gt;Cutting down on JavaScript is super important when you're building modern websites. It's a big deal for making your pages work well and be more efficient overall.&lt;/p&gt;

&lt;p&gt;As technology in software keeps growing, everyone wants websites to be quicker and work better, with less JavaScript taking up space. If you have extra JavaScript that you're not using, it just makes your web apps bigger and slower.&lt;/p&gt;

&lt;p&gt;This article will show you some tricks and tips to get rid of this extra JavaScript, so you can save time, make your website work faster, and make things run more smoothly.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is unused JavaScript?
&lt;/h2&gt;

&lt;p&gt;Simply put, unused JavaScript, often referred to as "dead code," is any code in your web app that the app doesn't use or require, yet it's still present in the JavaScript bundle sent to the browser. This idle code just hangs around, making your JavaScript bundle larger, and that can affect how well your website performs.&lt;/p&gt;

&lt;p&gt;There are a few reasons why you might have unused code in your JavaScript bundle.&lt;/p&gt;

&lt;p&gt;One common reason is that you added code during development, but as your project progressed, you ended up not needing it anymore. Unfortunately, you might forget to take it out when you send the final bundle to the browser. This unused code could be functions, classes, or variables that are just sitting there, not doing anything in your app.&lt;/p&gt;

&lt;p&gt;Another reason could be unused dependencies. This means you're using external JavaScript code in your project that you don't need. What's worse, these external bits of code might have their own unused JavaScript, adding even more unnecessary stuff to your project. It's like having extra baggage that your website doesn't need, and it can slow things down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing unused code:
&lt;/h2&gt;

&lt;p&gt;You can get rid of unused JavaScript in your web apps using a few methods. These tips and tricks will assist you in sending out JavaScript bundles that are stronger and work more efficiently on the web, whether you're using basic JavaScript or specific libraries/frameworks such as React, SolidJS, or Vue.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Splitting:
&lt;/h2&gt;

&lt;p&gt;Code splitting is like breaking down your JavaScript code into smaller, more manageable pieces. Then, you can load these pieces when you need them or all at once, making it so you don't have to load your entire JavaScript package every single time—just what you need.&lt;/p&gt;

&lt;p&gt;Imagine having just one JavaScript bundle that looks something like this:&lt;/p&gt;

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

&amp;lt;script src="main.js"&amp;gt;&amp;lt;/script&amp;gt; // 100kb file


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can split it into smaller chunks that can be downloaded only when needed:&lt;/p&gt;

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

&amp;lt;script async defer src="chunk1.js"&amp;gt;&amp;lt;/script&amp;gt; // 30 kb file
&amp;lt;script async defer src="chunk2.js"&amp;gt;&amp;lt;/script&amp;gt; // 30 kb file
&amp;lt;script async defer src="chunk3.js"&amp;gt;&amp;lt;/script&amp;gt; // 30 kb file


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This approach lessens the overall network load on the main thread, which is in charge of kicking off and fetching JavaScript scripts:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2fsxjcyah0vtinfazyyb.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2fsxjcyah0vtinfazyyb.jpeg" alt="JavaScript file splitting into chunk"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're working with a JavaScript library or framework such as Next.js or Vue, you don't have to do this yourself — many modern JavaScript frameworks automatically take care of code splitting.&lt;/p&gt;

&lt;p&gt;Yet, you can assign certain components for server-side use only. This helps the framework intelligently break down JavaScript into even smaller parts that don't need to be bundled with client-side JavaScript chunks.&lt;/p&gt;

&lt;p&gt;A lot of organizations put this strategy to use in real-world scenarios, showing how effective it is. Let's take a look at how code splitting works on the React website as an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyz5aac76fntss4bgilmu.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyz5aac76fntss4bgilmu.jpeg" alt="chunk in javascript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tree shaking:
&lt;/h2&gt;

&lt;p&gt;Tree shaking is about getting rid of useless code, which means cutting out the JavaScript that your app doesn't use. When you're putting together your JavaScript chunks to send to the browser, a bunch of popular bundlers like Webpack, Rollup, or Vite use a tree-shaking technique to make sure only the necessary stuff makes it into the mix.&lt;/p&gt;

&lt;p&gt;To make sure tree shaking works in your bundle, stick to the modern ES6 syntax in your JavaScript components. That means using the import and export syntax.&lt;/p&gt;

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

// default import
import Header from 'Components'
// named import
import { Header } from 'Components'

// default export
export default Header
// named export
export { Header }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The modern ES6 syntax assists your bundler in spotting dead code, much like how ESLint signals if there's an imported component that isn't used anywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript modification:
&lt;/h2&gt;

&lt;p&gt;Having less JavaScript in your bundle translates to a quicker download for the browser. To keep your JavaScript file as compact as possible, make sure to minify it before sending it out.&lt;/p&gt;

&lt;p&gt;Minifying JavaScript involves removing unnecessary elements like whitespaces, syntax highlighting, comments, and other parts of the code that aren't essential for the final production build. These extra bits can bloat the bundle with unused code.&lt;/p&gt;

&lt;p&gt;Even seemingly straightforward JavaScript code can be condensed and tweaked. Take a look at this example of basic JavaScript code before the minification process:&lt;/p&gt;

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

// multiply function
const multiply = (a, b) =&amp;gt; {
   return a * b
}

// call multiply function
multiply(3, 4)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here’s how this code looks after minification:&lt;/p&gt;

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

const multiply = (a, b) =&amp;gt; a * b;
multiply(3, 4);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Think about the impact minifying JavaScript could have on extensive bundles!&lt;/p&gt;

&lt;p&gt;Minifying JavaScript is simpler than you'd imagine. There are various online tools for JavaScript minification, like Terser, Uglify, babel-minify, and others that you can choose from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Load JavaScript asynchronously:
&lt;/h2&gt;

&lt;p&gt;Here's a handy tip that can save you a bunch of time on network bandwidth: always load your JavaScript asynchronously.&lt;/p&gt;

&lt;p&gt;You can achieve this by adding 'async' and 'defer' to your JavaScript scripts. This makes sure that downloading JavaScript won't hold up or stop your HTML from being parsed or displayed while the JavaScript is loading.&lt;/p&gt;

&lt;p&gt;Both 'async' and 'defer' manage the downloading and execution of JavaScript scripts, but they do it in a slightly different order. You can choose what works best for your project.&lt;/p&gt;

&lt;p&gt;An async JavaScript script works as shown in this image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdm5s36nwadqs7iwrl4to.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdm5s36nwadqs7iwrl4to.jpeg" alt="Asynchronous JavaScript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Meanwhile, a defer JavaScript script works like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnjf0pur8w0exg7yji9bd.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnjf0pur8w0exg7yji9bd.jpeg" alt="Defer JavaScript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In lots of situations, using both 'async' and 'defer' does the trick nicely. Here's an example of how you can write this:&lt;/p&gt;

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

&amp;lt;script async defer src="main.js"&amp;gt;&amp;lt;/script&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Dynamic Imports
&lt;/h2&gt;

&lt;p&gt;Now, with ES6 modules in plain JavaScript, you can pull off dynamic imports. These come in handy when you need to load JavaScript modules or scripts based on certain conditions. Here's how you'd write dynamic imports:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import('./helper.js')
  .then((module) =&amp;gt; {
    // use helper code here
  })
  .catch((error) =&amp;gt; {
    // catch errors
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This approach lets us ask for a JavaScript bundle once specific conditions are fulfilled, instead of importing every JavaScript component right at the beginning of the file. Check out this code snippet where the module loads only after the event listener is attached:&lt;/p&gt;


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

&lt;p&gt;document.getElementById('dashboard').addEventListener('click', () =&amp;gt; {&lt;br&gt;
  import('./helper.js')&lt;br&gt;
    .then((module) =&amp;gt; {&lt;br&gt;
      // Use helper module APIs&lt;br&gt;
      module.callSomeFunction();&lt;br&gt;
    })&lt;br&gt;
    // catch unexpected error here&lt;br&gt;
    .catch((error) =&amp;gt; {&lt;br&gt;
      console.error("Oops! An error has occurred");&lt;br&gt;
    });&lt;br&gt;
});&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Lazy loading technique&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Lazy loading in JavaScript is a simple yet super handy technique. When done right, it can save your network bandwidth. The basic idea is to only load the JavaScript modules that you need at that moment.&lt;/p&gt;

&lt;p&gt;One smart trick is to load JavaScript based on the height of the viewport. Let's say you have a super long list of users. It wouldn't make sense to load the details of, let's say, the 430th user when that info isn't visible on the current screen.&lt;/p&gt;

&lt;p&gt;Some cool libraries handle this. They make sure your JavaScript modules only load when a specific user, like the 430th, shows up on the screen.&lt;/p&gt;

&lt;p&gt;And guess what? Lazy loading isn't just for lists. It works for other things too, like images, videos, a bunch of nodes, and more. For instance, you can show a placeholder first, and then the actual image and its related JavaScript get downloaded by the browser.&lt;/p&gt;

&lt;p&gt;These tricks not only help in sending less JavaScript code but also make the user experience a whole lot better.&lt;/p&gt;

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

&lt;p&gt;In this article, we covered six techniques to send out less JavaScript and boost your project's performance.&lt;/p&gt;

&lt;p&gt;Remember, each project is unique with its architecture and patterns. While many of the tips we talked about should be helpful, not all may fit your specific situation.&lt;/p&gt;

&lt;p&gt;Trust your instincts to figure out what suits you and your project the best. This way, you can make your websites and apps stronger with confidence.&lt;/p&gt;

&lt;p&gt;I am Syket Bhattachergee, Software Engineer at CreoWis and Content Creator at &lt;a href="https://youtube.com/@syketb?si=wfID7P8hUsCvYs_J" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;. If you want to discuss your technical writing needs or any role? You can reach out to me on &lt;a href="https://linkedin.com/in/syketb" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and follow my work on &lt;a href="https://github.com/syket-git" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codequality</category>
      <category>performance</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
