rspec/rules/S6617/why-dotnet.adoc

10 lines
811 B
Plaintext
Raw Normal View History

== Why is this an issue?
When testing if a collection contains a specific item by simple equality, both `ICollection.Contains(T item)` and `IEnumerable.Any(x => x == item)` can be used. However, `Any` searches the data structure in a linear manner using a foreach loop, whereas `Contains` is considerably faster in some collection types, because of the underlying implementation. More specifically:
- `HashSet<T>` is a hashtable, and therefore has an O(1) lookup
- `SortedSet<T>` is a red-black tree, and therefore has a O(logN) lookup
- `List<T>` is a linear search, and therefore has an O(N) lookup, but the EqualityComparer is optimized for the `T` type, which is not the case for `Any`
For small collections, the performance difference may be negligible, but for large collections, it can be noticeable.