diff --git a/src/Core/src/Extensions/EnumerableExtensions.cs b/src/Core/src/Extensions/EnumerableExtensions.cs index 0dd36220ef9c..fb1fef33d8c0 100644 --- a/src/Core/src/Extensions/EnumerableExtensions.cs +++ b/src/Core/src/Extensions/EnumerableExtensions.cs @@ -56,6 +56,12 @@ public static int IndexOf(this IEnumerable enumerable, T item) if (enumerable == null) throw new ArgumentNullException(nameof(enumerable)); + if (enumerable is IList list) + return list.IndexOf(item); + + if (enumerable is T[] array) + return Array.IndexOf(array, item); + var i = 0; foreach (T element in enumerable) { diff --git a/src/Core/tests/Benchmarks/Benchmarks/IndexOfBenchmarker.cs b/src/Core/tests/Benchmarks/Benchmarks/IndexOfBenchmarker.cs new file mode 100644 index 000000000000..955533607b31 --- /dev/null +++ b/src/Core/tests/Benchmarks/Benchmarks/IndexOfBenchmarker.cs @@ -0,0 +1,30 @@ +namespace Microsoft.Maui.Benchmarks; + +using System.Collections.Generic; +using System.Linq; +using BenchmarkDotNet.Attributes; + +[MemoryDiagnoser] +public class IndexOfBenchmarker +{ + private List list; + private int[] array; + private HashSet hashSet; + + [GlobalSetup] + public void Setup() + { + list = Enumerable.Range(0, 1000000).ToList(); + array = list.ToArray(); + hashSet = list.ToHashSet(); + } + + [Benchmark] + public void IndexOfList() => EnumerableExtensions.IndexOf(list, 999999); + + [Benchmark] + public void IndexOfArray() => EnumerableExtensions.IndexOf(array, 999999); + + [Benchmark] + public void IndexOfHashSet() => EnumerableExtensions.IndexOf(hashSet, 999999); +} \ No newline at end of file