Automapper Performance Testing Automapper Performance Testing

I hate typing more lines of code then I need to; especially something as simple as mapping a domain model to a view model.  Enter Automapper!

By performing a one-liner:

 Mapper.Map<Customer, CustomerViewItem>(customer);
I can quickly map my domain models to my view models.

I was recently reviewing an old article on CodeProject: http://www.codeproject.com/Articles/61629/AutoMapper that contains a quick and easy demo.  Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping.  This test was done on 100,000 records and I must say I was shocked.

My first thought is this requires more testing.  Especially since 100,000 records is a lot.  In most scenarios I would estimate my largest mapping might be 1,000, but even 1 would probably be a very regular use-case.  Let’s put it to the test…


 I’ve created a simple console application and put it on GitHub here:

https://github.com/endyourif/AutomapperPerformance/

This program does the following:

  • Creates and populates a list of Customers
  • Times and automaps a Customer object to a CustomerViewItem
  • Times and manual maps a Customer object to a CustomerViewItem
  • Executes in a multiplication of 10, e.g. 1, 10, 100, 1000, 10000, and 100000

Here were the results I received:


AutoMapper with 1: 24
Manual Map with 1: 0
AutoMapper with 10: 0
Manual Map with 10: 0
AutoMapper with 100: 1
Manual Map with 100: 0
AutoMapper with 1000: 18
Manual Map with 1000: 0
AutoMapper with 10000: 112
Manual Map with 10000: 1
AutoMapper with 100000: 1070
Manual Map with 100000: 14

As you can see my results are very similar to the original CodeProject article results.  Here’s the catch, when the results are 1,000 or less the difference is negligible!  This is what becomes important; sure, if I’m doing a loop of 100,000 it’s over 1 second compared to a few milliseconds.

This is good to know.  I can now make an intelligent decision of when to use Automapper versus when to write it manually!

Summary

While Automapper is indeed consistently slower, when the list is 1,000 items or less I won’t even notice the difference.  If my list is in the 10,000+ there are clear slowdowns.  As I stated in my intro though, these are probably edge-cases that can be dealt with when the time comes – but I think it’s safe to assume that Automapper in an “average” project won’t inhibit you too much and save you writing lots of lines of code!

Published on May 27, 2013

Tags: Optimization | ASP.NET MVC and Web API Tutorial | c# | automapper

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.