AniUI Academy
hard+350 XPPractice

A buffer that batches then flushes

Firing a request for every click, keystroke or log line floods the network. The standard fix is a buffer with two triggers: flush as soon as enough items pile up, and flush anyway once the oldest item has waited long enough.

Implement a BatchBuffer class. new BatchBuffer(onFlush, options) takes a callback and an options object { size, waitMs }, defaulting to size 10 and waitMs 100. add(item) appends an item to the pending batch and returns the number of items pending afterwards. If adding brings the pending count up to size, flush immediately and synchronously, before add returns — so add returns 0 in that case. Otherwise, if this item is the first of a new batch, start a timer for waitMs milliseconds; adds after that must not restart the timer, so the batch is sent waitMs after its first item rather than after its last. flushNow() sends whatever is pending straight away and returns how many items it sent; when nothing is pending it does not call the callback and returns 0. pending() returns how many items are waiting.

Flushing means calling onFlush(items) once with a plain array of the pending items in the order they were added, then clearing the pending list and cancelling any timer. The callback is never called with an empty array.

What it has to do

  • Reaching size items flushes synchronously inside the add call.
  • A batch that never fills is flushed waitMs after its first item.
  • Later adds do not restart the timer.
  • flushNow() returns the number sent and does nothing when nothing is pending.
  • onFlush receives the items in the order they were added, and is never called with an empty array.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.