Q18: Implement a Time and Size Batch Request Scheduler
Build a batcher that accumulates incoming items and flushes them to a processing handler whenever maximum batch size is reached OR maximum delay time elapses.
Batch Request Scheduler
class BatchScheduler {
constructor(batchHandler, maxSize = 5, maxDelay = 100) {
this.handler = batchHandler;
this.maxSize = maxSize;
this.maxDelay = maxDelay;
this.batch = [];
this.timer = null;
}
add(item) {
this.batch.push(item);
if (this.batch.length >= this.maxSize) {
this.flush();
} else if (!this.timer) {
this.timer = setTimeout(() => this.flush(), this.maxDelay);
}
}
flush() {
if (this.timer) clearTimeout(this.timer);
this.timer = null;
if (this.batch.length > 0) {
const itemsToProcess = [...this.batch];
this.batch = [];
this.handler(itemsToProcess);
}
}
}Reaching maxSize triggers an immediate flush; otherwise a timer schedules a flush after maxDelay.
Key Takeaways
Dual trigger batching: flushes immediately on max size or automatically on timer expiration.
Optimizes network requests by combining multiple individual calls into single batch payload calls.
Knowledge Check
Why is the timer cleared inside flush() during max size execution triggers?