Skip to content
engineering workbench.by StudyPort Academy
Learning path
Real-World20 minLESSON 33 / 34

Performance: less unnecessary work

01

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.

Less work, with explicit tradeoffs
TechniqueWork it avoidsTradeoff to check
Move invariant workRepeating the same calculation in a loopThe inputs must truly remain unchanged
Batch DOM reads/writesRepeated layout recalculationKeep the visible result correct
DebounceWork during a burst of eventsAdds delay until the burst becomes quiet
ThrottleExcess frequency during continuous eventsChoose whether a final trailing update is needed
Cache/memoizeRecalculating a previously known resultMemory, comparison cost, and invalidation
Cancel or ignore stale requestsDisplaying obsolete resultsTie 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.

02

Mental model

FOLLOW THE VALUE

search("j")
schedule
pending j

The quiet-period timer starts.

01 / 03Trace the transformation

03

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.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Keep only the latest pending search

Cancel the previous timeout before scheduling the next one.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

js

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01When does this trailing debounce run?
    02What should happen before choosing an optimization?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson