Collection Performance: How Do You LINQ?

Way, way back in 2007, the Language-Integrated Query (LINQ) was introduced as part of .NET 3.5. Since LINQ was new, I spent some time learning how to create the most performant queries, especially for Entity Framework. I learned back then how the query is written can affect performance. In this article, I will show the most performant way to write queries for a collection of objects.

The LINQ API & LAMBDA’S

In the beginning of LINQ API, this is how we wrote a simple where query:

var query = from person in PersonCollection
           where person.Age.TotalDays > 1000
            select person;

var result = query.ToList() // This runs the query

This same query can be written this way using a LAMBDA:

var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).ToList();

Which one do you think is faster?

LINQ: WHERE() CHART
The speed of these two ways to write a simple where query is too close to call. Let’s see how the performance is for other common LINQ methods.

Any()

This LINQ Any() method returns a Boolean if there is even one item in the collection that meets the criteria. There are three ways to use Any().

// API
var query = from person in PersonCollection
           where person.Age.TotalDays > 1000
            select person;
      
var result = query.Any();    
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).Any();

OR

var result = PersonCollection.Any(p => p.Age.TotalDays > 1000);

Performance

LINQ: ANY() CHART
As you can see, using Any() as the first method in a LAMBDA call is faster!

First() & FirstOrDefault()

The method First() returns the first item in the collection that meets the criteria while FirstOrDefault() will also return a default value. Below are the different ways to use these methods.

// API
var query = from person in PersonCollection
           where person.Age.TotalDays > 1000
            select person;
      
var result = query.First();

var result = query.FirstOrDefault();       
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).First();

var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).FirstOrDefault();

OR

var result = PersonCollection.First(p => p.Age.TotalDays > 1000);

var result = PersonCollection.FirstOrDefault(p => p.Age.TotalDays > 1000);

Performance

LINQ: FIRST() CHART

LINQ: FIRSTORDEFAULT() CHART
As you can see, using First() or FirstOrDefault() as the first method a LAMBDA call is faster!

Last() & LastOrDefault()

The method Last() returns the last item in the collection that meets the criteria while LastOrDefualt() will also return a default value. Below are the different ways to use these methods.

// API
var query = from person in PersonCollection
           where person.Age.TotalDays > 1000
            select person;

var result = query.Last();

var result = query.LastOrDefault();
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).Last();

var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).LastOrDefault();

OR

var result = PersonCollection.Last(p => p.Age.TotalDays > 1000);

var result = PersonCollection.LastOrDefault(p => p.Age.TotalDays > 1000);

Performance

HOW DO YOU LINK-LAST-CHARTAs you can see, using Last() or LastOrDefault() as the first method in a LAMBDA call is much, much faster!

Summary

These benchmark results prove, without a doubt, that you should always use Any(), First(), FirstOrDefault, Last(), LastOrDefault or Where() at the beginning of a LAMBDA statement as shown below:

var result = PersonCollection.FirstOrDefault(p => p.Age.TotalDays > 1000);

I apologize if now you must go analyze your codebase for these methods and fix them if needed. It will increase the performance of your applications. If you have any comments, please make them below.

 

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.


The benchmark results in this article are only for your reference. Your results will be different, so make sure to benchmark your code using Benchmarkdotnet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.