Every collection has very similar performance optimization when you instance an instance of it. There are three different ways of creating an instance of a collection.
You can create an empty collection. All arrays, sets, and dictionaries have an empty init
method:
let array = [Int]() let set = Set<Int>() let dic = [String : Int]()
The other way is to instance an instance of a collection and reserve a required memory capacity. All collections have dynamic size, and they allocate more memory when needed. When you know how many elements you are going to store in the collection, it is useful to allocate exactly the required amount of memory upfront:
var array = [Int]() array.reserveCapacity(500_000) var set = Set<Int>(minimumCapacity: 500_000) var dic = [String : Int](minimumCapacity: 500_000)