<?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: Tushar Goswami</title>
    <description>The latest articles on DEV Community by Tushar Goswami (@goswamitushar).</description>
    <link>https://dev.to/goswamitushar</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%2F1713420%2Ff57417e1-b24d-406a-b33d-b4ec0c2f96ee.png</url>
      <title>DEV Community: Tushar Goswami</title>
      <link>https://dev.to/goswamitushar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/goswamitushar"/>
    <language>en</language>
    <item>
      <title>Debounced Search with Client-side Filtering: A Lightweight Optimization for Large Lists</title>
      <dc:creator>Tushar Goswami</dc:creator>
      <pubDate>Mon, 07 Apr 2025 18:59:46 +0000</pubDate>
      <link>https://dev.to/goswamitushar/debounced-search-with-client-side-filtering-a-lightweight-optimization-for-large-lists-2mn2</link>
      <guid>https://dev.to/goswamitushar/debounced-search-with-client-side-filtering-a-lightweight-optimization-for-large-lists-2mn2</guid>
      <description>&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rendering large datasets — like 1500+ city names in an autocomplete — can seriously affect performance. Without any optimization, each keystroke causes filtering + re-rendering, leading to lag and janky UX.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution: Debounced Client-side Search&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of triggering filtering on every keystroke, debounce the input. This way, filtering is only triggered after the user stops typing for a brief moment — leading to smoother UI and less CPU usage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Is Debouncing?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Debouncing delays a function’s execution until a certain time has passed since its last invocation. It’s useful for expensive operations triggered by rapid-fire events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function debounce(fn, delay) {
  let timer;
  return (...args) =&amp;gt; {
    clearTimeout(timer);
    timer = setTimeout(() =&amp;gt; fn(...args), delay);
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or simply use lodash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import debounce from 'lodash.debounce';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How I Used It in My Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I had an input field that allowed users to search through 1500+ city names. Here’s the straightforward solution I implemented:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [searchTerm, setSearchTerm] = useState('');
const [filteredCities, setFilteredCities] = useState([]);

const handleSearch = debounce((value) =&amp;gt; {
  const results = cities.filter(city =&amp;gt;
    city.toLowerCase().includes(value.toLowerCase())
  );
  setFilteredCities(results);
}, 300);

const handleChange = (e) =&amp;gt; {
  const value = e.target.value;
  setSearchTerm(value);
  handleSearch(value);
};

return (
  &amp;lt;input
    type="text"
    placeholder="Search city"
    value={searchTerm}
    onChange={handleChange}
  /&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach gave me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instant responsiveness while typing&lt;/li&gt;
&lt;li&gt;Zero jank, even with 1500+ entries&lt;/li&gt;
&lt;li&gt;No need to bring in heavy libraries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why This Is Effective&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method shines when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The list is large but manageable in memory&lt;/li&gt;
&lt;li&gt;You don’t need to paginate or scroll huge chunks of DOM&lt;/li&gt;
&lt;li&gt;Users expect fast results as they type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Use Debounced Search&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use this when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have a list that can be filtered entirely in memory&lt;/li&gt;
&lt;li&gt;You want to prevent excessive computations on every keystroke&lt;/li&gt;
&lt;li&gt;Your primary goal is input smoothness, not virtual rendering&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Don’t overengineer. For many use cases, client-side filtering + debounced input is all you need. It’s a simple, powerful trick to keep your app snappy and clean.&lt;/p&gt;

&lt;p&gt;Give it a try in your next search input — your users (and your performance metrics) will thank you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Full Code Snippet (Standalone Component)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import debounce from 'lodash.debounce';

const CitySearch = ({ cities }) =&amp;gt; {
  const [searchTerm, setSearchTerm] = useState('');
  const [filteredCities, setFilteredCities] = useState([]);

  const handleSearch = debounce((value) =&amp;gt; {
    const results = cities.filter(city =&amp;gt;
      city.toLowerCase().includes(value.toLowerCase())
    );
    setFilteredCities(results);
  }, 300);

  const handleChange = (e) =&amp;gt; {
    const value = e.target.value;
    setSearchTerm(value);
    handleSearch(value);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input
        type="text"
        placeholder="Search city"
        value={searchTerm}
        onChange={handleChange}
      /&amp;gt;
      &amp;lt;ul&amp;gt;
        {filteredCities.map((city, index) =&amp;gt; (
          &amp;lt;li key={index}&amp;gt;{city}&amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default CitySearch;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
