Copying a collection: ToList vs ToArray

  • Gérald Barré

It's common to use ToList() or ToArray() to copy a collection to a new collection. I've seen many comments on the web about which one is the most performant without any proof. So, it was time to run a benchmark.

C#
[MemoryDiagnoser]
public class CloneCollectionBenchmark
{
    private byte[] _array = null!;

    [Params(1, 10, 1_000, 10_000)]
    public int Size { get; set; }

    [GlobalSetup]
    public void Setup()
    {
        _array = Enumerable.Range(0, Size).Select(i => (byte)i).ToArray();
    }

    [Benchmark]
    public void Array_ToArray() => _array.ToArray();

    [Benchmark]
    public void Array_ToList() => _array.ToList();

    [Benchmark]
    public void Enumerable_ToArray() => AsEnumerable(_array).ToArray();

    [Benchmark]
    public void Enumerable_ToList() => AsEnumerable(_array).ToList();

    // Avoid optimizations as ToList or ToArray cannot know
    // the number of items in the collection to clone
    IEnumerable<T> AsEnumerable<T>(IEnumerable<T> collection)
    {
        foreach (var item in collection)
            yield return item;
    }
}
INI
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 7 5800X, 1 CPU, 16 logical and 8 physical cores
.NET SDK=6.0.100
  [Host]     : .NET 6.0.0 (6.0.21.48005), X64 RyuJIT
  DefaultJob : .NET 6.0.0 (6.0.21.48005), X64 RyuJIT
MethodSizeMeanErrorStdDevGen 0Gen 1Allocated
Array_ToArray118.14 ns0.201 ns0.188 ns0.0019-32 B
Array_ToList120.91 ns0.421 ns0.394 ns0.0038-64 B
Enumerable_ToArray155.16 ns0.845 ns0.790 ns0.0091-152 B
Enumerable_ToList145.69 ns0.911 ns1.217 ns0.0091-152 B
Array_ToArray1018.14 ns0.383 ns0.536 ns0.0024-40 B
Array_ToList1021.53 ns0.427 ns0.420 ns0.0043-72 B
Enumerable_ToArray10147.84 ns2.978 ns3.429 ns0.0134-224 B
Enumerable_ToList10133.48 ns2.239 ns2.199 ns0.0134-224 B
Array_ToArray100056.60 ns1.115 ns1.982 ns0.0612-1,024 B
Array_ToList100065.12 ns1.318 ns1.410 ns0.06310.00021,056 B
Enumerable_ToArray10006,868.17 ns107.927 ns100.955 ns0.1450-2,504 B
Enumerable_ToList10007,222.47 ns111.929 ns104.698 ns0.1373-2,384 B
Array_ToArray10000403.56 ns10.100 ns28.651 ns0.5984-10,024 B
Array_ToList10000391.92 ns7.772 ns19.065 ns0.60030.021010,056 B
Enumerable_ToArray1000067,017.62 ns1,159.196 ns1,084.313 ns1.5869-27,112 B
Enumerable_ToList1000065,375.11 ns1,246.528 ns1,224.257 ns1.95310.122133,200 B

If you only plan to iterate the result collection, you should use ToArray(). Indeed, foreach will be faster on an array than on a List<T>.

C#
foreach(var item in enumerable.ToArray())
{
    // ...
}

// Iterate a List<T> could be a few times slower than T[]
foreach(var item in enumerable.ToList())
{
    // ...
}
MethodSizeMeanErrorStdDevMedianGen 0Gen 1Allocated
ToArray_Foreach100002,671.84 ns42.267 ns39.537 ns2,666.25 ns0.5951-10,024 B
ToList_Foreach100007,958.46 ns261.928 ns772.300 ns7,523.37 ns0.59510.015310,056 B

#Conclusion

There is no significant difference between ToArray() and ToList() for most of the use-cases! But, it could be better to use ToArray() if the only use case is to iterate the result collection.

#Additional resources

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub