skip to content

When bids on an online auction's lot page burst to dozens a second, how should the page paint the ladder, price and watch count?

level: middleimportance: should knowfreq 45%

answer

  1. events are data, frames are paint
  2. one paint per frame
  3. latest value per field wins
  4. top of the ladder, not all of it
  5. a rounded count, not a crowd

basics

~20 s

Fold events into pending state where the latest value per field wins and the ladder keeps its top entries, then paint once per animation frame. Show watchers as a throttled, rounded number; announce only what the bidder must act on.

solid answer

~50 s

Events are data, not paint instructions. Each one updates a small **pending state**: the latest price wins, new bids join the ladder, which is capped to its top fifteen or so, and the newest bid is marked for a brief highlight. A single `requestAnimationFrame` callback then paints that state **once per frame**, however many events arrived since the last one. The **watch count** comes from the server every few seconds and renders rounded ("about 50,000 watching"), never as a list of names. For screen-reader users, the ladder is not a live region: a polite region states the price at most every few seconds, and "you've been outbid" is announced at once because it is time-critical. Because the pending state holds latest values and a capped list, it stays bounded even when a hidden tab stops getting frames.

code

javascript · 19 lines
javascript
const pending = { price: null, leader: null, ladder: [], watchers: null };
let frameRequested = false;

function onLotEvent(event) {            // called after sequence checks
  if (event.type === 'bid') {
    pending.price = event.amount;
    pending.leader = event.bidder;
    pending.ladder = [event, ...pending.ladder].slice(0, 15);
  } else if (event.type === 'watchers') {
    pending.watchers = event.count;
  }
  if (!frameRequested) {
    frameRequested = true;
    requestAnimationFrame(() => {
      frameRequested = false;
      paintLot(pending);                // one DOM update per frame
    });
  }
}

go deeper

for a junior

Recall that the screen only refreshes so many times a second, so painting every incoming event wastes work the user never sees.

for a middle

Explain folding events into pending state with latest-value-wins, painting once per animation frame, and showing presence as a throttled, rounded number.

for a senior

Keep coalescing separate from dropping so sequencing still works, bound the pending state for hidden tabs, and design announcements so assistive technology users hear what they must act on.

for a principal

Set a rate contract per topic between server and client: what the server coalesces, what the client coalesces, and which signals stay exact.

## The problem with painting every event In a popular lot's last two minutes, bids can arrive dozens of times a second, in bursts. A page that re-renders the ladder, price and countdown for every event does work the screen can never show: the display only refreshes a fixed number of times a second (most commonly 60), so updates between two frames are invisible, yet each one still costs layout and script time. The cost lands exactly when the bidder is trying to click, so the bid button responds late at the worst moment. The design separates two things: - **Applying an event** updates data: cheap, and done for every event, in sequence order. - **Painting** updates the screen: expensive, and done **at most once per frame**. ## Coalesce, then paint once per frame The client keeps a small **pending state** per lot, and each event folds into it: | Field | Rule when several events arrive in one frame | |---|---| | Current price and leader | the latest (highest sequence) wins | | Bid ladder | new bids are added; only the top 15 or so are kept for display | | Newest bid highlight | only the most recent one is highlighted | | Closing time | the latest extension wins | | Watch count | the latest server value wins | A `requestAnimationFrame` callback, scheduled once when the first change arrives and not again until it has run, reads the pending state and paints it. Ten events in one frame produce one paint, with the latest price and the correct top of the ladder. The browser calls that callback before its next repaint, which is why it beats a fixed timer: a timer is not aligned with frames, so it may paint twice in one frame or miss one. Two cautions keep this honest: 1. **Coalescing is not dropping.** Every event still advances the lot's sequence number and its data; only the paint is merged. Dropping events to "keep up" breaks gap detection and loses bids from the ladder. 2. **The pending state must stay bounded.** Animation-frame callbacks are paused in most browsers in background tabs. Because the state holds latest values and a capped list rather than a queue of every event, a hidden tab does not accumulate an ever-growing backlog, and one paint on return shows the current lot. ## The countdown under load The closing countdown is the one element that must stay smooth while bids burst. It is **derived**, not counted down: the lot read gives the server's end time, the page measures its clock offset from the server once, and each frame shows end time minus the corrected current time. A busy frame therefore cannot make the countdown drift, and a closing-time extension is just a new end time arriving as an event, applied through the same pending state as the price. ## Presence as a UI decision "N people watching" is **presence**: who is here now. In a small room it can be a list of faces; at 50,000 it is a number, and an approximate one: - the **server aggregates** it and sends it every few seconds on the lot's topic; - the client **rounds** it for display ("about 50,000", "1.2k"), and shows an exact figure only when small; - the client never receives individual joins and leaves for a hot lot, because at this scale that traffic can outweigh the bids. The number's job is social proof and urgency, so a few seconds of lag and rounding cost the user nothing. ## Accessible updates without a flood A screen-reader user cannot hear dozens of bids a second, and should not have to. - The **ladder is not a live region**; it is a list the user can read on demand. - A **polite** live region states the current price at most every few seconds; polite announcements generally wait rather than interrupting the current task. - **"You've been outbid"** with seconds left is time-critical, which is the case assertive announcements exist for; everything else stays polite. ## What it costs The page needs a per-lot pending state, one frame scheduler and a rounding rule for counts. It gains a steady frame rate under bursts, a bid button that responds when it matters, and a watch indicator that costs almost nothing to keep current.

  • Why not throttle painting with a timer every 100 ms instead?
    A timer is not aligned with the display's refresh, so it can paint twice in one frame or skip one, and a fixed 100 ms adds visible lag to the price. An animation-frame callback paints exactly once before each repaint, which is the most often the user can see a change anyway.
  • When would you show watchers as a list rather than a number?
    When the group is small and the people matter to each other, for example a private sale with a handful of invited bidders. Then a short list is useful and cheap. On a public lot with thousands of watchers, a rounded number is all the screen can usefully show.

A stadium scoreboard during a fast rally: the operator does not flash every intermediate score, they post the latest one on each refresh, and the crowd count on the board is rounded.

  • Re-render the ladder and price for every incoming bid event.
  • Skip events during a burst to keep the page responsive.
  • Show the full list of everyone watching the lot, updated live.
  • Put the bid ladder in a live region so screen-reader users hear every bid.
  • Queue every event for the next frame, however long the tab stays hidden.