Repository navigation
perf: search the strings of an array or a list directly in Contains - #1822
Merged
Merged
Conversation
`Contains` with a string advanced an enumerator, checked the cancellation and called the comparison virtually for every item, which cost about 9 ns per item, against about 3 ns in FluentAssertions and TUnit. On the 17 titles of the `Contains` benchmark this put aweXpect behind both. On .NET 8 and later, an array or a `List<T>` is now searched with a plain loop when the strings are compared ordinally. Only a comparison that runs no code of the caller searches directly, because a list detects a modification during the search only in its enumerator. An array or a list knows all its items, so the search cannot be canceled before their end. Measured on the 17 titles of the benchmark (ABBA, mean of two runs per side): | Method | Before | After | Allocated before / after | |---|---:|---:|---:| | `List<string>` `Contains` (first item) | 254.3 ns | 265.8 ns | 720 B / 720 B | | `List<string>` `Contains` (7th item, the benchmark) | 324.3 ns | 286.6 ns | 720 B / 720 B | | `List<string>` `Contains` (last item) | 388.9 ns | 302.9 ns | 720 B / 720 B | | `string[]` `Contains` (last item) | 363.8 ns | 278.6 ns | 720 B / 720 B | | `List<string>` `DoesNotContain` | 405.9 ns | 323.8 ns | 720 B / 720 B | The first item is within the noise.
|
Contributor
🚀 Benchmark ResultsDetails
|
Contributor
|
This is addressed in release v3.1.0. |
github-actions Bot
added a commit
that referenced
this pull request
Oct 11, 2026
… a list directly in `Contains` (#1822) by Valentin Breuß
github-actions Bot
added a commit
that referenced
this pull request
Oct 11, 2026
… a list directly in `Contains` (#1822) by Valentin Breuß
This was referenced Oct 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Containswith a string advanced an enumerator, checked the cancellation and called the comparison virtually for every item, which cost about 9 ns per item, against about 3 ns in FluentAssertions and TUnit. On the 17 titles of theContainsbenchmark this put aweXpect behind both.On .NET 8 and later, an array or a
List<T>is now searched with a plain loop when the strings are compared ordinally. Only a comparison that runs no code of the caller searches directly, because a list detects a modification during the search only in its enumerator. An array or a list knows all its items, so the search cannot be canceled before their end.Measured on the 17 titles of the benchmark (ABBA, mean of two runs per side):
List<string>Contains(first item)List<string>Contains(7th item, the benchmark)List<string>Contains(last item)string[]Contains(last item)List<string>DoesNotContainThe first item is within the noise.