min()
or max()
over sorted().first
or sorted().last
SW-P1011Using sorted().first
or sorted().last
can be inefficient, as the entire collection needs to be sorted, which is not required if we only need the minimum or maximum element. This can cause performance issues, especially when dealing with large collections.
.first(where:)
over .filter { }.first
in collections SW-P1007Using .first(where:)
instead of .filter { }.first
in collections, can result in cleaner and more efficient code.
.last(where:)
over .filter { }.last
in collections SW-P1009Using .filter {}.last
to get the last element of a collection is less efficient than using .last(where:)
. The former method first iterates through the entire collection to filter the elements and then returns the last element. This is less efficient because it allocates an intermediate buffer/allocation.
contains
over range(of:) != nil
and range(of:) == nil
SW-P1012Using range(of:) != nil
and range(of:) == nil
instead of contains
can lead to slower performance and less readable code.
contains
over comparing filter(where:).count
to zero SW-P1001Using the filter(where:)
method followed by the count
property to determine if a collection contains a specific element is not as efficient as using the contains
method directly.
reduce(into:_:)
over reduce(_:_:)
for copy-on-write types SW-P1010Using reduce(_:_:)
instead of reduce(into:_:)
for copy-on-write types can lead to unnecessary copies of the original collection, resulting in decreased performance and increased memory usage. This is because reduce(_:_:)
creates a new copy of the collection on each iteration,
.allSatisfy()
or .contains()
over reduce(true)
or reduce(false)
SW-P1000Using the reduce
method with a boolean value to check if any or all elements in a sequence satisfy a condition is less performant than using the .allSatisfy()
or .contains()
method.
contains
over using filter(where:).isEmpty
SW-P1002Using filter(where:).isEmpty
to determine if an array contains an element is a common mistake in Swift. It is an inefficient way of checking if an element exists in an array, as it requires iterating over the entire array
contains
over first(where:) != nil
and firstIndex(where:) != nil
SW-P1003Using the contains
method is a more readable and performant way to check if an element exists in an array instead of using first(where:) != nil
or firstIndex(where:) != nil
.
isEmpty
over comparing collection to an empty array or dictionary literal SW-P1004Using isEmpty
to check if a collection is empty is more efficient and concise than comparing the collection to an empty array or dictionary literal. Comparing collections to empty literals can lead to subtle bugs and
flatMap
over map
followed by reduce([], +)
SW-P1008Using map
followed by reduce
to transform and aggregate a collection requires two separate iterations over the collection. This can lead to performance issues when dealing with large collections. Instead, it is recommended to use flatMap
to achieve the same result in a single iteration.
isEmpty
over comparing count
to zero SW-P1005Using isEmpty
is a more efficient and readable way to check if a collection is empty than comparing its count
property to zero. When using count
to check if a collection is empty, if the collection does not implement RandomAccessCollection
, the runtime must iterate over all elements of the collection to count them before comparing the result to zero.
isEmpty
over comparing string
to an empty string literal SW-P1006It's better to check whether a string is empty or not using the isEmpty
property rather than comparing it to an empty string literal. Why is this an issue: Comparing a string to an empty string literal is less efficient than using the isEmpty
property.