Performance: less unnecessary work
Understand the idea
A search box sends a request on every keystroke and renders results nobody asked for anymore. The slow experience comes from avoidable work.
A budget of work the user waits for
A fast-looking helper may not matter if the real delay is a request, repeated page layout, or rendering thousands of items. Choose the interaction the user experiences and measure it before changing code. This keeps effort focused on work that actually affects the wait.
Measure the actual interaction
Measure a reproducible interaction before optimizing it. Use the browser's performance tools to distinguish script work, layout, rendering, and network waiting. A microbenchmark of one helper may not explain why a real page feels slow.
| Technique | Work it avoids | Tradeoff to check |
|---|---|---|
| Move invariant work | Repeating the same calculation in a loop | The inputs must truly remain unchanged |
| Batch DOM reads/writes | Repeated layout recalculation | Keep the visible result correct |
| Debounce | Work during a burst of events | Adds delay until the burst becomes quiet |
| Throttle | Excess frequency during continuous events | Choose whether a final trailing update is needed |
| Cache/memoize | Recalculating a previously known result | Memory, comparison cost, and invalidation |
| Cancel or ignore stale requests | Displaying obsolete results | Tie each result to the current interaction |
Avoid repeated calculations and layout
Look for repeated calculations inside loops, repeated reads mixed with writes, and renders that recompute unchanged results. Move invariant work outside a loop when the inputs truly stay fixed. In a UI framework, keep state ownership clear so an unrelated update does not force expensive work across the whole screen.
An invariant is a value that stays the same during a loop. Calculating it once outside the loop can avoid unnecessary repeated work. In a rendered interface, check whether an unchanged input forces the same expensive calculation again. A cache saves previous results, but needs an explicit rule for when those results stop being valid.
Debounce versus throttle
Debouncing waits for a quiet period before running work, which suits search input. Each new event replaces the pending call. Throttling allows work at a limited frequency during a continuous stream, which can suit scroll updates. Both require deliberate leading/trailing behavior and cleanup; they are different to the user.
A trailing debounce runs after events go quiet for a chosen delay; each new event replaces the pending call. A throttle limits how often work runs while events keep arriving. Think about the final event as well as the first: an update policy that never shows the final position or query is not correct merely because it does less work.
Protect the result, not just the timer
Delaying requests does not guarantee response order. Cancel obsolete work or ignore results whose request identity is no longer current. Do not add caching or memoization everywhere: cache invalidation, retained memory, and comparison costs can outweigh the saved calculation. Verify the actual interaction again after the change.
In the example, both calls happen immediately, so the second cancels the first pending timer. Only js is eventually logged. A real search also needs a plan for response order: debouncing reduces how many requests start, but it cannot guarantee that an older request finishes first.
Mental model
FOLLOW THE VALUE
The quiet-period timer starts.
01 / 03Trace the transformation
Live example
Both calls happen . Clearing the first timer leaves one trailing invocation with js. The short delay makes this demonstration easy to observe; choose a real delay from the interaction you measure.
Your console output appears here.
Guided challenge
Keep only the latest pending search
Cancel the previous timeout before scheduling the next one.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
js
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.