Community JavaScript Snippet

Batch and Coalesce Fetch Calls in React

Twenty cards on a page each ask for /api/users/:id and you cut server load 20x with this 30-line dataloader. The batch fires on the next microtask; same id never goes over the wire twice.

Batch and Coalesce Fetch Calls in React

Twenty cards on a page each ask for /api/users/:id and you cut server load 20x with this 30-line dataloader. The batch fires on the next microtask; same id never goes over the wire twice.

JavaScript
Frontend
3 snippets
batched-dispatch
react
performance
sophiesharma

By @sophiesharma

March 25, 2026

·

Updated May 18, 2026

813 views

22

4.4 (10)

Three moving parts: a pending queue, a one-shot scheduled flag, and queueMicrotask to fire the flush at the end of the current synchronous burst. The microtask boundary is the right granularity for React: between the time the first component's effect runs and the next paint, every component on the page has had a chance to enqueue its id. The new Set dedupe before the batch fetch is the second win after batching itself; the same user id requested by 5 cards still hits the API once. I have shipped this exact pattern to coalesce avatar lookups across a discussion thread; it cut requests by 12x without any component changes.