Performance Tips On FirstOrDefault Vs DictionaryKey

I would like to share one of the interesting performance tips when you deal with large collection sizes to retrieve the data. In your day-to-day programming life, we all are very eager to implement the requirements but we all fail at some point on performance measurement on the implemented logic/code. When your application grows big, there might be a chance of getting a memory leak issue and drops a performance when we don’t take a proper measurement of the code improvement.
 
Here we will see when we use FirstOrDefault and DictionaryKey to retrieve the data from the collection.
 
I am taking an example of the Aadhaar system, it has billions of Aadhaar entries that are been stored and I need to retrieve one specific Aadhaar details from a billion. Let’s give it a try with FirstOrDefault and DictionaryKey to retrieve the data from a billion Aadhaar entries.
 
Code walkthrough
 
Let’s see in code level and who will win!!!
 
Creating an Aadhaar seed of 1 million records,
 
Performance Tips on FirstOrDefault vs DictionaryKey
 
Here I have few samples of Aadhaar numbers and let’s evaluate the performance and which need to be used.

Performance Tips on FirstOrDefault vs DictionaryKey
 
Here is the execution time and output,
 
Performance Tips on FirstOrDefault vs DictionaryKey
 
I hope now you know that retrieving the data by a DictionaryKey is very very faster than FirstOrDefault() LINQ function. Here we see a very huge difference in execution time to retrieve the data in both ways.
 
One of the data was retrieved by FirstOrDefault() and it has taken ~20.02 ms. The same data is retrieved by a DictionaryKey and it has taken just ~0.0017 ms. Won’t it be very surprising!
 
It is always recommended to use the Dictionary collection when you are dealing with a large amount of data. FirstOrDefault() LINQ statement is suitable for the small amount of data.
 
Just taking another scenario, if you want to retrieve the following Aadhaar details like 40-50 records and store them in another collection. Just imagine, how much time will be taken to retrieve all 40-50 records by a FirstOrDefault() LINQ statement or by a DictionaryKey?. With a cumulative time of all 40-50 records by a FirstOrDefault() LINQ statement will be more rather than by a DictionaryKey data retrieval.
 
Both the implementation has it has its own advantages and we need to identify, how these data are frequently used, what could be the size of the collection, is all data are most used by all the time. Based on these criteria you can decide which one suits them best.