<?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: Chanakya sarma</title>
    <description>The latest articles on DEV Community by Chanakya sarma (@chanakyasarma).</description>
    <link>https://dev.to/chanakyasarma</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%2F1106166%2F62085146-0ef7-4986-8d55-41f76aab6db3.png</url>
      <title>DEV Community: Chanakya sarma</title>
      <link>https://dev.to/chanakyasarma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chanakyasarma"/>
    <language>en</language>
    <item>
      <title>🚀 Mastering Throttling and Debouncing in JavaScript: A Fun and Beginner-Friendly Tutorial!🏀</title>
      <dc:creator>Chanakya sarma</dc:creator>
      <pubDate>Thu, 20 Jul 2023 07:03:42 +0000</pubDate>
      <link>https://dev.to/chanakyasarma/mastering-throttling-and-debouncing-in-javascript-a-fun-and-beginner-friendly-tutorial-o1p</link>
      <guid>https://dev.to/chanakyasarma/mastering-throttling-and-debouncing-in-javascript-a-fun-and-beginner-friendly-tutorial-o1p</guid>
      <description>&lt;p&gt;Hey there, fellow code enthusiasts! Today, we're going to explore two important concepts in JavaScript: throttling and debouncing. Don't worry if those words sound a bit intimidating at first; I'll explain everything in a way that even beginners can grasp easily! 🤓&lt;/p&gt;

&lt;p&gt;🤔 What are Throttling and Debouncing?&lt;/p&gt;

&lt;p&gt;Throttling and debouncing are techniques used to control the frequency of certain events in JavaScript, especially when dealing with user interactions like scrolling, resizing, or button clicks. By utilizing these techniques, we can improve performance, prevent unnecessary function calls, and create smoother user experiences. Let's dive into each concept with the help of emojis and code snippets! 🏊‍♂️&lt;/p&gt;

&lt;p&gt;⏱️ Throttling - The Stopwatch Technique ⏱️&lt;/p&gt;

&lt;p&gt;Imagine you have a stopwatch, and every time you press a button, the stopwatch records the time. However, you have a rule that says you can only record the time once every 2 seconds, no matter how many times you press the button within that timeframe. This is exactly what throttling does!&lt;/p&gt;

&lt;p&gt;In coding terms, throttling limits the rate at which a function is called, ensuring it can't be invoked more frequently than a specified time interval. Let's see how it works with this emoji-powered example:&lt;br&gt;
&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;// Throttle function implementation&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;throttle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&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;currentTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTime&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentTime&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example usage:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;emojiThrottle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;throttle&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🚀 Throttled function called!&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="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Call this function whenever needed, but it won't be invoked more frequently than once every 2 seconds.&lt;/span&gt;
&lt;span class="nx"&gt;emojiThrottle&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Throttling in Real-Life Application: Scroll Events&lt;br&gt;
When a user scrolls down a webpage, a scroll event is triggered multiple times as they scroll. Without throttling, this could lead to performance issues as the browser tries to handle too many scroll events in a short time. By applying throttling, we can ensure that the scroll event is processed at a controlled rate, preventing excessive function calls.&lt;/p&gt;

&lt;p&gt;🔵 Debouncing - The Bouncing Ball 🏀&lt;/p&gt;

&lt;p&gt;Imagine you have a bouncy ball, and every time you throw it against the wall, a timer starts. If you throw the ball again before the timer runs out, the previous timer resets. Only when the timer finally reaches zero will an action be triggered. That's debouncing for you!&lt;/p&gt;

&lt;p&gt;Debouncing is useful when you want to delay the execution of a function until after a certain quiet period. It's perfect for scenarios like search bars or auto-saving, where you want to avoid unnecessary API calls during continuous user input. Let's check out the emoji-powered code snippet:&lt;br&gt;
&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;// Debounce function implementation&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timeoutId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeoutId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;timeoutId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;delay&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="c1"&gt;// Example usage:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;emojiDebounce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;debounce&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🏀 Debounced function called!&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Call this function whenever needed, but it will execute only after 1 second of inactivity.&lt;/span&gt;
&lt;span class="nx"&gt;emojiDebounce&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Debouncing in Real-Life Application: Search Bar Autosuggestions&lt;br&gt;
When a user types in a search bar, you may want to show autosuggestions based on the input. If you make an API call for every keystroke, it could result in multiple unnecessary API requests. Debouncing can be used here to delay the API call until the user has stopped typing, improving the efficiency of the autosuggestions feature.&lt;/p&gt;

&lt;p&gt;🌟 Conclusion&lt;/p&gt;

&lt;p&gt;Congratulations! You've now grasped the concepts of throttling and debouncing in JavaScript. Throttling helps limit the rate of function calls, while debouncing postpones a function's execution until after a quiet period. These techniques can enhance your web applications' performance and deliver a smoother user experience.&lt;/p&gt;

&lt;p&gt;Remember, choosing the right approach depends on the specific use case. So, keep practicing and experimenting with these techniques to become a JavaScript master! 💪&lt;/p&gt;

&lt;p&gt;Happy coding! 🎉😄👩‍💻&lt;/p&gt;

&lt;p&gt;Remember, the best way to learn is by experimenting and building your own projects. I hope this fun and easy guide has shed some light on the exciting world of throttling and debouncing in JavaScript! So go ahead, try out the code snippets, and have fun exploring these concepts further.&lt;/p&gt;

&lt;p&gt;Keep coding, keep learning, and remember to add your own creative touch to your projects. Happy coding! 🚀🎨&lt;/p&gt;

</description>
      <category>throttling</category>
      <category>debouncing</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>🧹 The Magic of JavaScript Garbage Collector: Keeping Your Code Clean! 🪄</title>
      <dc:creator>Chanakya sarma</dc:creator>
      <pubDate>Thu, 29 Jun 2023 06:21:40 +0000</pubDate>
      <link>https://dev.to/chanakyasarma/the-magic-of-javascript-garbage-collector-keeping-your-code-clean-eb1</link>
      <guid>https://dev.to/chanakyasarma/the-magic-of-javascript-garbage-collector-keeping-your-code-clean-eb1</guid>
      <description>&lt;p&gt;Welcome, curious coder! Today, we embark on an exciting journey into the enchanting world of JavaScript garbage collection. 🚀✨&lt;/p&gt;

&lt;p&gt;🌟 &lt;strong&gt;What is JavaScript Garbage Collection?&lt;/strong&gt;&lt;br&gt;
Imagine your codebase as a bustling city with countless objects and variables moving around. Over time, some of these objects become unused and take up valuable memory. This is where the JavaScript garbage collector comes to the rescue!&lt;/p&gt;

&lt;p&gt;The garbage collector is like a diligent street sweeper that continuously identifies and removes unused objects, freeing up memory and keeping your codebase tidy. It ensures your JavaScript programs run smoothly without memory leaks. 🧹💫&lt;/p&gt;

&lt;p&gt;🧪 &lt;strong&gt;Let's Experiment: Memory Cleanup in Action!&lt;/strong&gt;&lt;br&gt;
To better understand garbage collection, let's experiment with a simple code example. Imagine we have a function that creates a large array and assigns it to a variable:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SEmv20FG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r1ha88ro5afwtcptajc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SEmv20FG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r1ha88ro5afwtcptajc.png" alt="Code for garbage collection" width="800" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this scenario, our &lt;strong&gt;createBigArray&lt;/strong&gt; function generates an array with a million fiery elements. However, once the &lt;strong&gt;createBigArray&lt;/strong&gt; function finishes executing, the &lt;strong&gt;bigArray&lt;/strong&gt; variable is no longer accessible. Will the garbage collector catch this?&lt;/p&gt;

&lt;p&gt;Of course! The garbage collector will recognize that &lt;strong&gt;bigArray&lt;/strong&gt; is no longer reachable from any root object and will mark it for removal. When the next garbage collection cycle kicks in, the memory occupied by &lt;strong&gt;bigArray&lt;/strong&gt; will be freed, ensuring our codebase stays neat and efficient. 🔥🧹💪&lt;/p&gt;

&lt;h2&gt;
  
  
  🌌 Garbage Collection Triggers
&lt;/h2&gt;

&lt;p&gt;Now that we understand how the garbage collector works, it's essential to know when it springs into action. JavaScript implementations use different strategies to decide when to trigger garbage collection. Some common triggers include:Reference Counting and Mark-and-Sweep algorithm.&lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Reference Counting:&lt;/strong&gt; This approach keeps track of how many references an object has. When the count reaches zero, indicating there are no more references, the object is removed. However, this method is not widely used due to certain limitations, such as circular references.&lt;/p&gt;

&lt;p&gt;Here's how the Reference Counting algorithm works:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8YG_7FGp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7bktp7szbjqef9e1ucr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8YG_7FGp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7bktp7szbjqef9e1ucr.png" alt="Code for reference counting" width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1️⃣ Reference Creation: Whenever a reference is created to an object, the reference count of the object is incremented by one.&lt;/p&gt;

&lt;p&gt;2️⃣ Reference Deletion: When a reference to an object is removed or goes out of scope, the reference count of the object is decremented by one. For example:&lt;/p&gt;

&lt;p&gt;3️⃣ Reaching Zero: When the reference count of an object reaches zero, it means that no references exist to that object. At this point, the object is considered garbage, and its memory can be freed.&lt;/p&gt;

&lt;p&gt;4️⃣ Deallocation: Once an object's reference count reaches zero, the garbage collector can deallocate the memory occupied by the object. The memory is then available for reuse by other objects.&lt;/p&gt;

&lt;p&gt;🚫 &lt;strong&gt;Limitations of Reference Counting&lt;/strong&gt;&lt;br&gt;
While the Reference Counting algorithm is straightforward, it has some limitations:&lt;/p&gt;

&lt;p&gt;1️⃣ Overhead: Maintaining reference counts for every object incurs additional overhead during object creation, assignment, and deletion. This overhead can impact performance.&lt;/p&gt;

&lt;p&gt;2️⃣ Circular References: The Reference Counting algorithm struggles with circular references, where objects reference each other in a loop. In such cases, the reference counts of the objects involved never reach zero, even if they are no longer reachable from the root. This can lead to memory leaks, as circularly referenced objects are never garbage collected.&lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Mark-and-Sweep with Heuristics:&lt;/strong&gt; Most modern JavaScript engines employ a combination of the Mark-and-Sweep algorithm with additional heuristics. These heuristics monitor factors like memory consumption, CPU usage, and idle time to determine the optimal moments to initiate garbage collection.&lt;/p&gt;

&lt;p&gt;🚮 &lt;strong&gt;Let's Understand Mark-and-Sweep Algorithm in Depth&lt;/strong&gt;&lt;br&gt;
Underneath the hood, JavaScript uses a clever algorithm called the Mark-and-Sweep algorithm for garbage collection. Let's dive into this fascinating process step by step:&lt;/p&gt;

&lt;p&gt;1️⃣ Mark: The garbage collector first starts with a root object, such as the global object or an object explicitly referenced by your code. It then traverses all the reachable objects from the root, marking them as active.&lt;/p&gt;

&lt;p&gt;2️⃣ Sweep: Once the marking phase is complete, the garbage collector sweeps through the entire heap, finding any unmarked objects. These unmarked objects are considered unused and ready for removal.&lt;/p&gt;

&lt;p&gt;3️⃣ Memory Reclamation: Finally, the garbage collector reclaims the memory occupied by the unused objects, making it available for future use. This ensures that memory remains efficient and effective.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Tip:&lt;/strong&gt; Avoiding Memory Leaks&lt;br&gt;
While JavaScript's garbage collector does an excellent job, it's still essential to write clean code to avoid memory leaks. Here are a few tips:&lt;/p&gt;

&lt;p&gt;🔸 Always release references to objects or variables you no longer need.&lt;br&gt;
🔸 Be mindful of event listeners and remove them when they are no longer required.&lt;br&gt;
🔸 Avoid circular references, as they can confuse the garbage collector.&lt;/p&gt;

&lt;p&gt;Remember, clean code is efficient code! 🧹✨&lt;/p&gt;

&lt;p&gt;✨ &lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Congratulations on completing this magical journey through JavaScript garbage collection! You now have a solid foundation to understand how the garbage collector keeps your codebase sparkling clean. By leveraging this knowledge, you can write more efficient and robust JavaScript code. 🎉💪&lt;/p&gt;

&lt;p&gt;So go forth, code wizards, and create magnificent programs while the garbage collector takes care of the memory sweep! Happy coding! 🧙‍♀️🔮🌟&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>garbagecollector</category>
      <category>productivity</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
