General Performance Tip: Optimizing Method Returns

Often, when invoking a method that retrieves a collection, such as from a database, there may be instances where nothing is returned. Historically, many developers, myself included, would resort to returning a null value to signify the absence of items, as illustrated in the following example:

return null;

Empty Array Return Methods

First, let’s examine various methods for returning an empty array. Here’s the approach utilized in the benchmark tests. Firstly, we have the conventional method commonly employed by developers to return an empty array:

return new string[0];

You can also utilize an empty expression:

return [];

Lastly, Array.Empty() can be employed:

return Array.Empty<string>();

Next, let’s look at the benchmark results.

Benchmark Results

As observed, employing Array.Empty() and an expression yield nearly identical performance, whereas the conventional approach is 4.23 times slower.

Exploring Methods to Return Reference Type Collections

Now, let’s explore various methods to return a collection of reference types. Here are several approaches to achieve this, including utilizing an array. The example below demonstrates the usage of Enumerable.Empty():

return Enumerable.Empty<Person>();

In more recent practices, many developers prefer to return an empty collection instead, eliminating the need for a null check, as demonstrated in the following example:

return new List<Person>();

Alternatively, you can utilize the default keyword, as illustrated in this example:

return default;

If you have not utilized the default keyword before, it serves the purpose of retrieving the default value associated with a specific data type. The default value is assigned to a variable when no explicit value is provided. The default keyword is commonly employed in generic programming and scenarios where initializing a variable with its default value is necessary.

Next, let’s examine the performance implications of these approaches.

Benchmark Results

As evident from these results, returning null exhibits the best performance, yet it’s often discouraged as a common practice. Returning using Array.Empty() is 1.26 times faster than Enumerable.Empty(), 4.26 times faster than creating a new array, and 4.8 times faster than returning an empty List.

Additionally, it’s common to return an Exception if there aren’t any results. However, this is an expensive process, being 3,904 times slower than returning an empty array.

For effective type design, it is advisable to consistently return an empty collection or array. If your intention is to return a collection while handling exceptions simultaneously, I suggest exploring the SimpleResult<> type in Spargine,

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

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

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

2 thoughts on “General Performance Tip: Optimizing Method Returns

  1. You should also benchmark:

      return Array.Empty<Person>();
      return Enumerable.Empty<Person>();
      return new Person[0];

    These will, of course, require the return value data type to be IEnumerable<Person> instead of IList<Person>.

Leave a comment

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