0
0
Rest APIprogramming~5 mins

Why batch operations reduce round trips in Rest API - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why batch operations reduce round trips
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations
  • 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.
How Execution Grows With Input

As the number of items grows, the number of requests grows the same way in the first method.

Input Size (n)Approx. Operations (Requests)
1010 requests
100100 requests
10001000 requests

Pattern observation: The number of requests grows directly with the number of items, making it slower as n grows.

Final Time Complexity

Time Complexity: O(n)

This means the time to complete grows directly with the number of items when sending requests one by one.

Common Mistake

[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.

Interview Connect

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.

Self-Check

"What if we split the items into smaller batches instead of one big batch? How would the time complexity change?"