Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Frontend System Design Roadmap
  1. 1.HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)
  2. 2.TCP and TLS Handshakes: Connection Setup Costs
  3. 3.Browser Network Stack and Connection Pooling
  4. 4.GET vs POST and Idempotency in HTTP
  5. 5.The Critical Rendering Path (CRP)
  6. 6.DOM, CSSOM and the Render Tree
  7. 7.Layout, Paint and Compositing
  8. 8.Reflows and Repaints Explained
  9. 9.Event Loop: Call Stack, Task and Microtask Queues
  10. 10.Async JavaScript: Promises and async/await
  11. 11.Web Workers: Multi-threading in the Browser
  12. 12.Browser Storage: Cookies, Web Storage, IndexedDB
  13. 13.Client-Side Storage: Limits and Security Risks
  14. 14.Choosing the Right Client-Side Storage
  15. 15.HTTP Caching: Cache-Control, ETag, Last-Modified
  16. 16.Browser-Level Caching Mechanisms
  17. 17.Cache Invalidation for Frontend Apps
  18. 18.Structuring a Large Single-Page App
  19. 19.Component-Based Architecture Principles
  20. 20.Props, State and Component Lifecycle
  21. 21.Module Bundling Basics
  22. 22.Core Web Vitals: LCP, INP and CLS
  23. 23.FCP and Time to Interactive (TTI) Explained
  24. 24.Measuring Performance: Lighthouse and WebPageTest
  25. 25.Asset Compression and Minification Basics
  26. 26.What Is a CDN and How Does It Work?
  27. 27.CDN Benefits: Latency, Availability, Offload
  28. 28.CDN Cache Behavior and Invalidation
  29. 29.Global State vs Local Component State
  30. 30.State Patterns: Redux, Zustand and Context
  31. 31.Data Fetching: React Query and SWR
  32. 32.Immutable Data in Frontend State
  33. 33.SSR vs CSR: Rendering Trade-offs
  34. 34.Hydration and Time to First Byte (TTFB)
  35. 35.Static Site Generation (SSG) for Speed and SEO
  36. 36.Incremental Static Regeneration (ISR)
  37. 37.Micro-Frontends: Core Principles
  38. 38.Micro-Frontend Integration Strategies
  39. 39.Communication Between Micro-Frontends
  40. 40.Micro-Frontends: Pros and Cons for Large Teams
  41. 41.Real-Time: Polling, SSE and WebSockets
  42. 42.Real-Time Use Cases: Chat, Alerts, Live Data
  43. 43.Designing a Real-Time Data Flow
  44. 44.Scaling WebSocket Connections
  45. 45.Responsive Images: srcset, sizes and picture
  46. 46.Modern Image Formats: WebP, AVIF, JPEG XL
  47. 47.Lazy Loading Images and Videos
  48. 48.Video Streaming: HLS, DASH, Adaptive Bitrate
  49. 49.REST and GraphQL for Frontend Engineers
  50. 50.Batching, Throttling and Debouncing API Calls
  51. 51.Handling API Errors in the Frontend
  52. 52.Authentication Flows: OAuth and JWT
  53. 53.Global Error Boundaries in React
  54. 54.Error Reporting: Sentry and LogRocket
  55. 55.Graceful Degradation and Fallback UIs
  56. 56.Real User Monitoring (RUM)
  57. 57.CORS: Cross-Origin Resource Sharing
  58. 58.Preventing XSS (Cross-Site Scripting)
  59. 59.CSRF Protection for Frontend Apps
  60. 60.Content Security Policy (CSP)
  61. 61.Storing Auth Tokens: Cookies vs localStorage
  62. 62.PWA Manifest and Installability
  63. 63.Service Workers: Lifecycle and Caching Strategies
  64. 64.Offline Support and Background Sync
  65. 65.Push Notifications for Web Apps
  66. 66.Webpack vs Vite vs Parcel
  67. 67.Code Splitting, Tree Shaking, Asset Optimization
  68. 68.Module Resolution and Hot Module Replacement
  69. 69.Monorepo Tooling: Nx and Lerna
  70. 70.Atomic Design for Component Libraries
  71. 71.Theming and Customization in Design Systems
  72. 72.Accessibility in Component Libraries
  73. 73.Component Library Docs, Testing and Versioning
  74. 74.Publishing and Consuming a Component Library
  75. 75.Full-Text Search: Levenshtein and Fuzzy Matching
  76. 76.Indexing Large Datasets in the Browser
  77. 77.Search Performance: Debouncing and Web Workers
  78. 78.Integrating Backend Search Services
  79. 79.Choosing a Chart Library: D3, Chart.js, Recharts
  80. 80.Rendering Large Datasets: Canvas vs SVG
  81. 81.Interactive, Responsive Data Visualizations
  82. 82.Data Fetching and Aggregation for Dashboards
  83. 83.A/B Test Design and Rollout Strategies
  84. 84.Client-Side vs Server-Side A/B Testing
  85. 85.Feature Flags and Remote Configuration
  86. 86.Experiment Data Collection and Analytics
Frontend System Design Roadmap
77

Frontend System Design · Lesson 77 of 86 · 4 min

Search Performance: Debouncing and Web Workers

This is how you show you can build a UI that stays responsive under load, a core expectation for any senior role.

Debounce user input to prevent wasteful work

A naive search input triggers a search on every keystroke. This is wasteful. It hammers your backend with useless requests for "a", "ap", "app", and overwhelms the main thread with rendering work if the search is client-side.

The solution is to wait until the user pauses typing. This is called debouncing. A debounced function will only execute after a specified period of inactivity. If the event fires again, the waiting period resets.

For a search input, a delay of 300ms is a common starting point. It feels responsive but filters out the noise of continuous typing.

The trap: You might be asked to write a debounce utility from scratch. Candidates often forget to handle the this context or correctly pass arguments to the original function.

function debounce<F extends (...args: any[]) => any>(func: F, delay: number): (...args: Parameters<F>) => void {
  let timeoutId: ReturnType<typeof setTimeout> | null = null;

  return function(this: ThisParameterType<F>, ...args: Parameters<F>) {
    const context = this;

    if (timeoutId) {
      clearTimeout(timeoutId);
    }

    timeoutId = setTimeout(() => {
      func.apply(context, args);
    }, delay);
  };
}

This implementation correctly clears any pending timer, captures the this context using apply, and forwards all arguments.

Offload heavy computation to a Web Worker

Debouncing reduces the frequency of search operations, but what if a single search is so expensive it still freezes the UI? This happens with large client-side indexes, like those discussed in the previous lesson, where searching might involve complex filtering or scoring that takes more than 50-100ms.

Anything that blocks the main thread for that long ruins the user experience. The fix is to move the computation off the main thread using a Web Worker.

A Web Worker runs a script in a background thread. The main thread communicates with it by sending and receiving messages. The UI remains fully interactive while the worker is busy.

The key limitations are that workers cannot access the DOM, and data passed via postMessage is copied, not shared. This serialization/deserialization has a cost, so workers are not a free lunch. They are for genuinely heavy tasks, not trivial ones.

In an interview, when asked how to handle a search that freezes the UI, your answer is a Web Worker. Explain the postMessage API and the benefit of keeping the main thread free for rendering and user interaction. Acknowledge the data-copying overhead as a trade-off.

Handle out-of-order worker responses

Using a worker introduces a potential race condition. Imagine a user types "A", then quickly deletes it and types "B".

  1. Main thread sends { query: "A" } to the worker.
  2. Main thread sends { query: "B" } to the worker.
  3. The search for "B" is fast and returns. The UI updates with results for "B".
  4. The search for "A" was slower but now finishes. It returns, and the UI incorrectly updates with results for "A".

The user is now looking at stale results.

The fix: Implement a request ID system. The main thread should maintain a counter. Increment it for every new search request and send the ID along with the query. The worker includes this ID in its response. The main thread then ignores any message whose ID does not match the most recently sent ID.

This ensures you only ever display results for the user's latest query. Mentioning this pattern shows you anticipate and solve for asynchronous race conditions.

Distinguish debounce from throttle

These terms are often confused, and using them precisely matters.

Debounce: Executes a function only after a period of inactivity. Good for search input, where you only care about the final value.

Throttle: Executes a function at most once every X milliseconds. Good for rate-limiting events that fire continuously, like scroll or resize, where you want periodic updates but not a flood of them.

If you were building an infinite scroll feature, you would throttle the scroll handler, not debounce it. Using debounce would mean your "load more" function would only fire after the user completely stopped scrolling.

Try it

Implement a React hook, useDebouncedValue, that takes a value and a delay. It should return a new version of the value that only updates after the specified delay has passed. Ensure your implementation cancels any pending timeouts in its cleanup effect to prevent memory leaks when the component unmounts.

Next: Now that you can run searches efficiently, we'll look at how to connect your client to a dedicated backend with Integrating Backend Search Services.

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice system design problems
Previous

Indexing Large Datasets in the Browser

Next

Integrating Backend Search Services