Why batch operations reduce round trips in Rest API - Performance Analysis
When working with APIs, sending many requests one by one can slow things down.
We want to understand how batch operations help by reducing the number of trips between client and server.
Analyze the time complexity of the following code snippet.
// Sending multiple requests one by one
for (let i = 0; i < items.length; i++) {
await api.sendRequest(items[i]);
}
// Sending all items in one batch request
await api.sendBatchRequest(items);
This code shows two ways to send data: one request per item, or all items together in one batch.
- Primary operation: Sending a request to the server.
- How many times: In the first case, once per item (loop runs n times). In the batch case, only once regardless of item count.
As the number of items grows, the number of requests grows the same way in the first method.
| Input Size (n) | Approx. Operations (Requests) |
|---|---|
| 10 | 10 requests |
| 100 | 100 requests |
| 1000 | 1000 requests |
Pattern observation: The number of requests grows directly with the number of items, making it slower as n grows.
Time Complexity: O(n)
This means the time to complete grows directly with the number of items when sending requests one by one.
[X] Wrong: "Sending many small requests is just as fast as one big request."
[OK] Correct: Each request has overhead like network delay, so many small requests add up and slow down the process.
Understanding how batch operations reduce round trips shows you can think about efficiency beyond just code logic.
This skill helps you write faster, more scalable programs that work well in real-world networks.
"What if we split the items into smaller batches instead of one big batch? How would the time complexity change?"