{"id":58071,"date":"2023-08-23T17:50:07","date_gmt":"2023-08-23T12:20:07","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58071"},"modified":"2023-08-31T17:56:34","modified_gmt":"2023-08-31T12:26:34","slug":"event-optimization-using-debounce","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/event-optimization-using-debounce\/","title":{"rendered":"Event optimization using debounce"},"content":{"rendered":"<h2>What is Debounce?<\/h2>\n<p>Debounce is an optimization technique to reduce the frequency of repetitive function calls.<\/p>\n<p>It is especially suitable for cases where we have some function that can be called multiple times within a short duration, but only the latest execution result is required.<\/p>\n<p>There are many ways to implement it. The general concept is we take a function and attach a timer to it. Now, whenever the method gets called, the timer starts, and the code executes when the timer finishes. If it gets called again before the ongoing timer completes, the previous call is rejected (the logic will not run), and the timer starts again, repeating the process. It happens till the timer completes. Then, only the logic is executed.<\/p>\n<div><\/div>\n<p><strong>Let&#8217;s take an example to get more clarity.<\/strong><\/p>\n<p>Assume there is a search box in the UI. We want to make the search results look real-time. The code is such that for every input from the user, an API call is made to the server, and upon receiving a response, we show the result in the UI.<\/p>\n<p>It is a perfect scenario for debounce as we want the search to appear in real-time, but only the result of the latest input is required. In short, we can skip the in-between API calls. Let&#8217;s see how.<\/p>\n<p>I have created a simple app using React. Just like our example, it has a search input.<\/p>\n<div><\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58065\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image1.png\" alt=\"\" width=\"322\" height=\"385\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image1.png 516w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image1-251x300.png 251w\" sizes=\"(max-width: 322px) 100vw, 322px\" \/><\/div>\n<div><\/div>\n<div><\/div>\n<p><strong>We also have a search component.<\/strong><\/p>\n<h1 data-pm-slice=\"1 1 []\" data-en-clipboard=\"true\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58066\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image2.png\" alt=\"\" width=\"564\" height=\"437\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image2.png 857w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image2-300x232.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image2-768x594.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image2-624x483.png 624w\" sizes=\"(max-width: 564px) 100vw, 564px\" \/><\/h1>\n<p>It has an input field, and on any change in the input, it calls the fetchData function. fetchData calls the API and sets the result to show in the UI.<\/p>\n<p>The API that I&#8217;m using is for movies. So let&#8217;s search &#8220;Dark Knight&#8221;&#8230;<\/p>\n<p>We have the results, but notice the number of API calls made. There are eleven API calls (one for each character).<\/p>\n<div><\/div>\n<h1 data-pm-slice=\"1 1 []\" data-en-clipboard=\"true\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58067\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image3.png\" alt=\"\" width=\"597\" height=\"491\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image3.png 798w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image3-300x247.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image3-768x631.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image3-624x513.png 624w\" sizes=\"(max-width: 597px) 100vw, 597px\" \/><\/h1>\n<div><\/div>\n<h3>Let&#8217;s use debounce here.<\/h3>\n<p>I have created a higher-order function (debounce). A higher-order function is a function that takes another function as an argument or returns a function.<\/p>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58068\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image4.png\" alt=\"\" width=\"377\" height=\"230\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image4.png 487w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image4-300x183.png 300w\" sizes=\"(max-width: 377px) 100vw, 377px\" \/><\/div>\n<p><span data-preserver-spaces=\"true\">The debounce function takes two arguments &#8211;<br \/>\n<\/span><\/p>\n<ol>\n<li><span data-preserver-spaces=\"true\">func (The function to debounce. We&#8217;ll call it the <em>original <\/em><em>function<\/em>)<br \/>\n<\/span><\/li>\n<li><span data-preserver-spaces=\"true\">delay (time for setTimeout)<\/span><\/li>\n<\/ol>\n<div>It then returns a new function. We&#8217;ll call it the <em>debounced\u00a0function<\/em>.<\/div>\n<div>This\u00a0<em>debounced function<\/em> has access to the\u00a0<em>timer\u00a0<\/em>variable. Whenever this\u00a0<em>debounced function<\/em> is called, it clears the timer value, if any, and then starts a setTimeout and assigns it to the timer. When the timer completes, it calls the\u00a0<em>original function<\/em>.<\/div>\n<div><\/div>\n<div>\n<p><span data-preserver-spaces=\"true\">Let&#8217;s go back to our Search component and make the necessary changes.<\/span><\/p>\n<ol>\n<li><span data-preserver-spaces=\"true\">We&#8217;ll create a debounced version of the fetchData function (debouncedFetchData).<\/span><\/li>\n<li><span data-preserver-spaces=\"true\">Whenever the input changes, call debouncedFetchData instead of fetchData.\n<p><\/span><\/li>\n<\/ol>\n<\/div>\n<h1 data-pm-slice=\"1 1 []\" data-en-clipboard=\"true\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58069\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image5.png\" alt=\"\" width=\"590\" height=\"370\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image5.png 790w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image5-300x188.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image5-768x481.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image5-624x391.png 624w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/h1>\n<div><\/div>\n<p>Okay, now let&#8217;s try the search again.<\/p>\n<p>This time, there were only four API calls compared to the eleven before.<\/p>\n<p data-pm-slice=\"1 1 []\" data-en-clipboard=\"true\">\u00a0<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58070\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image6.png\" alt=\"\" width=\"666\" height=\"493\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/image6.png 890w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image6-300x222.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image6-768x568.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/image6-624x461.png 624w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/p>\n<div>\n<h3>Conclusion<\/h3>\n<p>Event handling is an integral part of front-end development. Anytime we work with repetitive events that we can group, debounce might fit the use case perfectly. It is simple to implement yet very effective, and it can save server costs and make the application more performant.<\/p>\n<p>Please refer to our blogs for more insightful content.<\/p>\n<\/div>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>What is Debounce? Debounce is an optimization technique to reduce the frequency of repetitive function calls. It is especially suitable for cases where we have some function that can be called multiple times within a short duration, but only the latest execution result is required. There are many ways to implement it. The general concept [&hellip;]<\/p>\n","protected":false},"author":1613,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[3429,3038,1994],"tags":[5353],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58071"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1613"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58071"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58071\/revisions"}],"predecessor-version":[{"id":58249,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58071\/revisions\/58249"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}