Connect with us

Articles

Swift Array vs. Set: A Head-To-Head Comparison in Performance

Let them race and see what will happen.

Introduction

In Swift programming, three primary collection types are commonly used: DictionaryArray and Set. As shown below, Array is a collection of ordered values, Set is a collection of unique values, while Dictionary is an unordered collection of key-value pairs.

They are all generic data types as discussed in one of my previous Medium articles. As a type-safe language, Swift requires that these collection types specify the data type of their elements to work with, preventing us making a silly mistake like trying to append an integer to a string array, insert a string to an integer set, or add a String-Int pair to a dictionary using integer keys.

There are similarities between Array and Set. From the above figure, it’s very clear that Array and Set are different from Dictionary in that their elements are individual values while the Dictionary’s are key-value pairs. As collection types, there are several similar methods/properties, like count and isEmpty, between Array and Set, which allows a head-to-head comparison possible. Because of these functional similarities, they are also used “interchangeably” in practice. However, whether there is a difference in their performance hasn’t been widely recognized.

There are also differences between Array and Set. As far as I know, Array is a preferred collection type over Set by many developers. I don’t know the exact reasons, but the possible reasons may include Array being an ordered set such that its elements can be conveniently accessed by using their indices, and its flexibility of element types (e.g., int, string, tuple, array). However, as an unordered collection of unique elements, Set does have its unique characteristics, particularly the requirement of its elements conforming to the Hashable protocol, which should supposedly make it faster to retrieve the needed hashed elements.

Despite these similarities and differences, I haven’t seen exact data on the direct comparisons in their performance between Array and Set in terms of their routine usage. Thus, the purpose of the current article is to provide some preliminary performance data of Array and Set, shedding some light on the optimal usage of these collection types in your Swift projects.

Conclusions

Overall, Set outperforms Array in various aspects when the task is related to the access to individual or all elements (e.g., find element index, find min and max values). This advantage comes from the Set’s conformation to the Hashable protocol. However, this conformation is a double-edged sword, as the Set is slower than the Array during the initialization involving the hash process for the Set. Nevertheless, the operations between Set and Array were comparable for the most part.

The take-home message is that when the order of the elements doesn’t matter, you may want to use Set instead of Array. However, it should also be noted that the elements in a Set need to be unique, while Array doesn’t have this restriction. When the order of the elements matters, you definitely want to choose Array as the desired collection type.

Full Article: Yong Cui @ The Startup

Advertisement

Trending