Async batch processing in Rest API - Time & Space Complexity
When working with async batch processing, it is important to understand how the time to complete tasks grows as the number of items increases.
We want to know how the total time changes when processing many requests in batches asynchronously.
Analyze the time complexity of the following code snippet.
async function processBatch(items) {
const batchSize = 5;
for (let i = 0; i < items.length; i += batchSize) {
const batch = items.slice(i, i + batchSize);
await Promise.all(batch.map(item => processItem(item)));
}
}
This code processes items in batches of 5, running each batch's items asynchronously, waiting for all to finish before moving to the next batch.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over items in batches and processing each batch asynchronously.
- How many times: The loop runs about n / batchSize times, where n is the number of items.
As the number of items grows, the number of batches grows roughly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 batches of 5 items each |
| 100 | 20 batches of 5 items each |
| 1000 | 200 batches of 5 items each |
Pattern observation: The total number of batches grows linearly with input size, so total work grows steadily as more items are added.
Time Complexity: O(n)
This means the total time to process all items grows roughly in direct proportion to the number of items.
[X] Wrong: "Because items are processed asynchronously in batches, the total time stays the same no matter how many items there are."
[OK] Correct: Even though items in a batch run at the same time, batches run one after another, so total time still grows with the number of batches, which depends on total items.
Understanding how async batch processing scales helps you explain how to handle many requests efficiently and shows you can reason about real-world code performance.
"What if we processed all items fully in parallel without batching? How would the time complexity change?"
Practice
async batch processing in REST APIs?Solution
Step 1: Understand async batch processing purpose
Async batch processing sends many requests together instead of one at a time.Step 2: Identify the benefit
Sending requests together saves time and improves efficiency.Final Answer:
It allows sending multiple requests at once to save time. -> Option AQuick Check:
Async batch processing = multiple requests at once [OK]
- Thinking async batch slows down requests
- Believing requests are processed one by one
- Confusing async batch with disabling parallelism
Solution
Step 1: Check fetch syntax for POST with JSON body
fetch requires method and body as JSON string for POST requests.Step 2: Identify correct option
fetch('/batch', { method: 'POST', body: JSON.stringify(requests) }) uses method 'POST' and JSON.stringify to convert requests to string.Final Answer:
fetch('/batch', { method: 'POST', body: JSON.stringify(requests) }) -> Option CQuick Check:
fetch POST with JSON body = fetch('/batch', { method: 'POST', body: JSON.stringify(requests) }) [OK]
- Passing body without JSON.stringify
- Using GET method with body
- Wrong fetch parameter order
const batchRequests = [
fetch('/api/data1'),
fetch('/api/data2'),
fetch('/api/data3')
];
Promise.all(batchRequests)
.then(responses => responses.map(r => r.status))
.then(statuses => console.log(statuses));Solution
Step 1: Understand Promise.all with fetch
Promise.all waits for all fetch requests to complete and returns an array of responses.Step 2: Map responses to their status codes
Each response has a status property, usually 200 if successful.Final Answer:
[200, 200, 200] -> Option BQuick Check:
Promise.all fetch statuses = [200, 200, 200] [OK]
- Expecting promises instead of resolved responses
- Trying to access status before Promise.all resolves
- Confusing map on promises vs responses
async function batchFetch(urls) {
const results = [];
urls.forEach(async url => {
const res = await fetch(url);
results.push(await res.json());
});
return results;
}Solution
Step 1: Analyze async inside forEach
forEach does not wait for async callbacks, so results may be empty on return.Step 2: Identify correct fix
Use for...of loop with await or Promise.all to wait for all fetches.Final Answer:
Using forEach with async does not wait for all fetches to finish. -> Option AQuick Check:
forEach async does not await = Using forEach with async does not wait for all fetches to finish. [OK]
- Assuming forEach waits for async callbacks
- Not returning or awaiting promises properly
- Misplacing results array declaration
Solution
Step 1: Understand batch size control
Sending all 100 requests at once may overload the server; batching limits concurrency.Step 2: Implement batch processing
Split URLs into groups of 10, then await Promise.all for each batch sequentially.Final Answer:
Split URLs into chunks of 10, then use Promise.all on each chunk sequentially. -> Option DQuick Check:
Batch requests in chunks with Promise.all = Split URLs into chunks of 10, then use Promise.all on each chunk sequentially. [OK]
- Sending all requests at once causing overload
- Not awaiting async calls properly
- Sending requests one by one losing async benefits
Ready to prove it?
Pass the test and this pattern moves unknown
