
.NET: CopyToAsync vs WriteAsync: The Benchmark You Didn’t Know You Needed
I had a task where one small part involved writing a byte array to a MemoryStream . Since this was a trivial task, I let Copilot generate the code. But Copilot used CopyToAsync instead of WriteAsync . That raised a question, so I asked why. It kept trying to convince me that CopyToAsync was a better fit for this scenario (a 1 MB file), and I quote: CopyToAsync might offer better performance due to its internal optimizations for copying data between streams. The performance difference between these two approaches can be minimal for small to moderately sized data. According to Copilot: Small to medium data: 12 KB – 32 KB Large data: 5 MB – 10 MB But here’s the important detail: I wasn’t copying between two independent streams. I already had a byte array in memory. In that case, wrapping it in a MemoryStream just to call CopyToAsync adds an extra abstraction layer. So I was skeptical. Copilot was confident. I was suspicious. As a responsible developer, I couldn’t sleep until I proved my p
Continue reading on Dev.to
Opens in a new tab

