{
  "id": 13928689,
  "title": "Why Await Inside a Loop Can Make Your Async Code 5x Slower",
  "url": "https://urgent.news/2026/10/12/why-await-inside-a-loop-can-make-your-async-code-5x-slower",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-12T10:10:41.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/dhanagani_lakshmi_2487ad0/why-await-inside-a-loop-can-make-your-async-code-5x-slower-3eca"
  },
  "original_language": "en",
  "account": "A common mistake in asynchronous programming involves calling multiple independent asynchronous methods inside a loop with await on each call. This approach is often slower than expected. For instance, five one-second calls taking about five seconds when using await, while Task.WhenAll can complete the same tasks in about one second. This article explores the reasons behind this performance difference and highlights four common issues that arise when running everything at once.\n\nTo simulate a database or web call, the source code uses Task.Delay(1000), which stands in for a one-second operation. The first test measures the time taken to execute five calls one after another using a loop. The second test uses Task.WhenAll to run all five calls simultaneously, and the third test compares this to running 100 tasks using Task.WhenAll.\n\nWhen awaiting a call within a loop, subsequent calls must wait for the preceding ones to finish, resulting in a cumulative wait time. In contrast, Task.WhenAll starts all tasks at once and waits for the slowest one to complete. This means that the total waiting time is dominated by the longest single wait, rather than the sum of all waits.\n\nInterestingly, running a hundred waits using Task.WhenAll takes almost the same time as running five waits. This is because a traditional await on a database or network call does not keep a thread occupied while it waits. The elapsed time measurement demonstrates this behavior, showing that the timing is consistent with the expectation that waiting does not consume significant resources.\n\nHowever, there are four issues that can arise when running everything at once:\n\n1. The external service may have limits. Making a hundred simultaneous calls to a real API or database could overwhelm the service or exhaust its connection pool. To mitigate this, you can limit the number of concurrent calls using SemaphoreSlim or Parallel.ForEachAsync with a maximum degree of parallelism.\n\n2. If multiple tasks fail, await Task.WhenAll(...) only rethrows the first exception. The remaining exceptions are present but not accessible. To handle all exceptions, you can capture the inner exceptions of the combined task and iterate through them.\n\nBy understanding these performance implications and addressing the common pitfalls, developers can optimize their asynchronous code and avoid unnecessary slowdowns.",
  "summary": "Here is a mistake that compiles, runs, returns correct results, and quietly makes your code about five times slower than it needs to be: calling several independent async methods inside a loop with await on each one. I measured it. Five one-second calls took about five seconds in a loop and about one second with Task.WhenAll . This article shows the numbers, explains why, and covers the four…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}