Use Array or List?

Hi there!

What is the best (most efficient, etc.) way to decide whether to use an Array or a List?

What I usually do is if I need to edit it in the script, I use a list. If I set it in the inspector and I am not editing in my script then I use an array.

Are there any performance considerations? Garbage collection? Usability?

Any answers are welcome!

If you add something regularly, use a list. If you roughly know how many items will be in it, preallocate memory space to reduce garbage allocation at runtime.

If something won’t change in size or will do so irregularly (like at level change), use an array since it is faster

For me it generally just boils down to: do I need to resize the collection or not.

That said for inspector-set collections which I’m usually not resizing at runtime, I seem to have a habit of using List<T> still.

Mind you I imagine the difference in overhead is not going to make a perceivable difference unless things are happening at a particularly large scale.

2 Likes

Just wondering, is there any difference (if you are not changing it) between array and list except performance?
Why would a list be preferable?

A List is backed by an array. The only performance difference when adding elements lies in the double boundary check: one occurs in the list and the other in the underlying array. However, this performance impact is practically negligible.

When a list is created, its initial array is assigned a size of 4 upon adding the first element. If you attempt to add a fifth element, the list will create a new array with double the size (8), copy all elements from the previous array to the new one, and allow the old array to be garbage collected. This “resizing” process introduces a noticeable performance difference between arrays and lists.

Every time you add an element that exceeds the capacity of the underlying array, a new array is created with double the size, and the contents are copied over.

To mitigate this overhead, you can initialize a list with a specified capacity. For example:
List<int> foo = new List<int>(420);

This initializes the list with an array capable of holding 420 elements. As you add elements, the garbage collector won’t need to handle array resizing until you try to add the 421st element. At that point, a new array with a capacity of 840 elements will be created, and the previous array will be garbage collected after its contents are copied.

In practice, the only significant performance consideration is the garbage collection of the old array. Choose the data structure that best suits your needs:

  • Use a list when frequently adding or removing elements.
  • Use an array when working with a fixed number of elements.

If performance is a concern, consider initializing your list with the maximum expected number of elements. Not only does this improve performance, but it also promotes “clean code” by roughly indicating the expected size of the list to anyone reading your code.

7 Likes

Wow, great answer.
I’ll leave the thread open in case anybody else pitches in, but this is what I was looking for.

Thanks!

2 Likes

So this is the next generic Array vs List thread. It’s not like we had this in the past :slight_smile:
2011, 2016, 2019, 2020, 2021
Those are just the first google results. I know there are at least 10 more -.-

What to use depends completely on the usecase. So such generic questions usually have not much value.

1 Like

Might I ask what the added value of this response is as a moderator?

To me, if this thread is redundant, just closing the thread and maybe linking past issues sounds like a better response as someone from Unity (or affiliated with Unity, not sure if you are employed by them).

I’m not affiliated with Unity at all. I’m just a random german guy who started using Unity around 2010 where I started answering questions on Unity Answers. I’m not even in game dev anymore. I learned most quirks about Unity by answering questions.

Yes, we have countless redundant threads and usually there’s no need to forcefully close them. Maybe if the OP provides more context of his concrete case we can give a more focused answer. We usually only close threads when they get out of hand or are constantly necro-posted.

My post was supposed to be a subtle reminder to first search for a solution before throwing out a question as suggested by the “Content Quality” section of the Code of conduct and to ask specific / detailed questions.

2 Likes

Got it! Having the moderator tag makes you seem quite official (;

The explanation is appreciated. It could be interpreted as a toxic/salty reply. I definitely agree people should google first and put research into it as well.