Skip to content

List.contains performance #15720

@jindraivanek

Description

@jindraivanek

Hi,
we've found that List.contains x xs is slower than List.exists (fun y -> x = y) xs, details in my blog post: https://jindraivanek.hashnode.dev/curious-case-of-listcontains-performance

Summary:

Would you be in favor to change List.contains implementation to

    let inline contains value source =
        source |> tryFind (fun x -> value = x) |> Option.isSome

?

Which turns out fastest from variants I tried. (In benchmark it is List.containsTryFind).

I can extend benchmarks with more cases if needed.

I can make PR for this.

--

Note:
Benchmarks was done on

BenchmarkDotNet=v0.13.4, OS=Windows 10 (10.0.19045.3208)
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
.NET SDK=8.0.100-preview.6.23330.14
  [Host]     : .NET 7.0.9 (7.0.923.32018), X64 RyuJIT AVX2 DEBUG
  DefaultJob : .NET 7.0.9 (7.0.923.32018), X64 RyuJIT AVX2

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions