๐ฆ๐๐ฝ๐ฒ๐ฟ๐ฐ๐ต๐ฎ๐ฟ๐ด๐ถ๐ป๐ด ๐๐๐๐ป๐ฐ ๐ถ๐ป ๐#: ๐๐ผ๐บ๐ฝ๐ฎ๐ฟ๐ฒ ๐ฆ๐ฒ๐พ๐๐ฒ๐ป๐๐ถ๐ฎ๐น ๐๐. ๐ฃ๐ฎ๐ฟ๐ฎ๐น๐น๐ฒ๐น ๐๐ถ๐๐ต ๐ง๐ฎ๐๐ธ.๐ช๐ต๐ฒ๐ป๐๐น๐น โก
Hey Devs! ๐ Have you ever wanted to call multiple services or run multiple tasks at once without waiting for each one to finish before starting the next? Thatโs exactly where ๐ง๐ฎ๐๐ธ.๐ช๐ต๐ฒ๐ป๐๐น๐น comes to the rescue! Instead of running each task one by one (which can be slow if each task takes a while), you can fire them all simultaneously, wait for all to complete, and then process the results in bulk.
Below, we have a ๐๐ฆ๐ฏ๐ค๐ฉ๐ฎ๐ข๐ณ๐ฌ๐๐ฐ๐ต๐๐ฆ๐ต setup that measures:
- ๐ฆ๐ฒ๐พ๐๐ฒ๐ป๐๐ถ๐ฎ๐น ๐ฟ๐ฒ๐พ๐๐ฒ๐๐๐ (one after another).
- ๐ฃ๐ฎ๐ฟ๐ฎ๐น๐น๐ฒ๐น ๐ฟ๐ฒ๐พ๐๐ฒ๐๐๐ (all at once using ๐๐ข๐ด๐ฌ.๐๐ฉ๐ฆ๐ฏ๐๐ญ๐ญ).
By measuring both approaches, weโll see how ๐๐ข๐ด๐ฌ.๐๐ฉ๐ฆ๐ฏ๐๐ญ๐ญ can significantly reduce the overall time when dealing with I/O-bound tasks such as HTTP calls. Letโs dive in! ๐โโ๏ธ
๐ช๐ต๐ ๐๐๐ฒ ๐๐๐จ๐ .๐๐๐๐ฃ๐ผ๐ก๐ก? ๐ก
๐ง๐ถ๐บ๐ฒ ๐ฆ๐ฎ๐๐ถ๐ป๐ด๐ If each request takes 500 ms, and you have 10 requests, a sequential approach might end up taking ~5000 ms. With ๐๐ข๐ด๐ฌ.๐๐ฉ๐ฆ๐ฏ๐๐ญ๐ญ, it could take around 500 ms total (assuming all 10 run simultaneously)!
๐๐น๐ฒ๐ฎ๐ป๐ฒ๐ฟ ๐๐ผ๐ฑ๐ฒ Instead of a big ๐ง๐ฐ๐ณ๐ฆ๐ข๐ค๐ฉ that awaits every single call, you collect all tasks in a list and await them together. Easy to read and maintain!
๐๐ฒ๐๐๐ฒ๐ฟ ๐ฅ๐ฒ๐๐ผ๐๐ฟ๐ฐ๐ฒ ๐จ๐๐ถ๐น๐ถ๐๐ฎ๐๐ถ๐ผ๐ป While one request is waiting on network I/O, others can proceed. This helps maximize concurrency without writing complex multi-threaded logic.
๐๐ ๐ฝ๐ฒ๐ฐ๐๐ฒ๐ฑ ๐ข๐๐๐ฐ๐ผ๐บ๐ฒ & ๐ง๐ฎ๐ธ๐ฒ๐ฎ๐๐ฎ๐๐ ๐ค ๐ฆ๐ฒ๐พ๐๐ฒ๐ป๐๐ถ๐ฎ๐น (๐๐ฆ๐ต๐๐ญ๐ญ๐๐ฆ๐ฒ๐ถ๐ฆ๐ฏ๐ต๐ช๐ข๐ญ๐๐ด๐บ๐ฏ๐ค): Runs each HTTP-like call one by one, typically summing all delays (e.g., 10 ร 500 ms).
๐ฃ๐ฎ๐ฟ๐ฎ๐น๐น๐ฒ๐น (๐๐ฆ๐ต๐๐ญ๐ญ๐๐ข๐ณ๐ข๐ญ๐ญ๐ฆ๐ญ๐๐ด๐บ๐ฏ๐ค): Initiates all tasks at once and waits for them together, drastically reducing total time if each call is I/O-bound.
Feel free to share your thoughts or experiences with parallel async calls! Do you have any real-world story of boosting performance with ๐๐ข๐ด๐ฌ.๐๐ฉ๐ฆ๐ฏ๐๐ญ๐ญ? Let us know in the comments! ๐