
Expressions are now an absolute part of a developer’s everyday life in .NET thanks to Linq.
However, due to their nature, expressions are not one of the very best performing tools, which is why the .NET team is also doing a lot to improve general performance while maintaining usability.
Performance Improvements in .NET 7
In simpler words, expressions are a markup language for how the runtime should traverse a “tree” to get properties or perform comparisons. This traversal takes time. And the more often you do it, the more time it takes.
However, expressions can also be compiled so that the traversal only has to be done once and the runtime otherwise has a plan of what to do. Very similar to how database statements (plans) are also cached.
Afterwards you only have to use the plan and save the overhead - and it is really big!
Unfortunately, it is not currently possible to shift the expression entirely to compile-time, but at least the overhead can be shifted to a one-time operation. Not to mention that compiling itself also takes time.
Sample Code
As an example for my benchmark I have chosen a very simple expression, so that you can see that even with the simplest expressions the effect is present. However, the more complex the expression, the higher the effect!
So we see an expression that only performs an Int-based comparison to get an element.
1 _persons.Where( person => person.Age == 23).Single();
To perform a compilation the expression must first be taken out of the operation itself, for example as a static field.
1 private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;
This expression alone can actually be used.
1 public Person Ex() => _persons.Where(s_ageExpression).Single();
However, we wanted to compile our expression, for which we can again use a static field.
1 private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;
2 private static readonly Func<Person, bool> s_ageExpressionCompiled = s_ageExpression.Compile();
Benchmark
So, as a result, we compare two things:
- A Where-query with a simple expression and one with a compiled expression
1 private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;
2 [Benchmark]
3 public Person Ex() => _persons.Where(s_ageExpression).Single();
4
5 // Compiled
6 private static readonly Func<Person, bool> s_ageExpressionCompiled = s_ageExpression.Compile();
7 [Benchmark(Baseline = true)]
8 public Person Ex_Compiled() => _persons.Where(s_ageExpressionCompiled).Single();
The result is very clear: the compiled query is over 3000 more performant.
1BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19044.2006/21H2/November2021Update)
2AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
3.NET SDK=7.0.100-rc.1.22431.12
4 [Host] : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
5 DefaultJob : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
6
7
8| Method | Mean | Error | StdDev | Ratio |
9|------------ |--------------:|-------------:|-------------:|---------:|
10| Ex | 228,941.16 ns | 4,508.268 ns | 4,823.797 ns | 3,464.82 |
11| Ex_Compiled | 66.10 ns | 1.258 ns | 1.346 ns | 1.00 |
Full Sample and Results: Benjamin Abt - Sustainable Code - Compiled Expressions
Conclusion:
It can be very worthwhile to compile certain expressions, the effect and performance improvements are gigantic.
Related articles

Mar 10, 2026 · 15 min read
.NET NuGet Trusted Publishing with GitHub Actions
Publishing NuGet packages has traditionally required one uncomfortable compromise: a long-lived API key had to exist somewhere in the …

Mar 09, 2026 · 7 min read
C# 15 Unions: Unions are finally in .NET
After many years of workarounds, design discussions and library-level substitutes, unions are finally becoming a first-class part of C#. The …

Mar 02, 2026 · 19 min read
Unio: High-Performance Discriminated Unions for C#
C# is a powerful language, but there is one road it has not yet fully paved: native discriminated union types. Developers have been working …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

Comments