Click here to Skip to main content
15,889,116 members
Articles / DevOps / Testing

Automapper Performance Testing

Rate me:
Please Sign up or sign in to vote.
3.61/5 (6 votes)
30 Apr 2015CPOL2 min read 30.1K   4   7
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:

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 introduction 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 from writing lots of lines of code!

This article was originally posted at http://endyourif.com/automapper-performance-testing

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
SuggestionAutomapper was improved Pin
Norberto Quesada Reyes29-May-17 7:17
Norberto Quesada Reyes29-May-17 7:17 
NewsSpeed no longer an issue! Pin
MetalKid00727-Oct-16 7:14
MetalKid00727-Oct-16 7:14 
Questionalternatives to automapper Pin
jecaestevez6-May-16 3:31
jecaestevez6-May-16 3:31 
QuestionAnother Alternative Pin
Eric Swann22-Feb-16 6:53
Eric Swann22-Feb-16 6:53 
I would take a look at Mapster...at the end of the day it does what 95% of people need out of AutoMapper, setup is similar, and it's ~20x faster: [Link]

I'm not a believer in micro-optimization unless the cost is ~0...and in this case the cost is typically 0 unless AutoMapper is a standard for your organization. There are some other good alternative mappers out there like ExpressMapper. EmitMapper has gotten kind of long in the tooth and isn't actively maintained anymore.

Disclaimer: It's a project I started when we ran into perf issues with AutoMapper under load at a previous job. We're now on 2.0 with multiple contributors and it's actively maintained and ready for DNX.
QuestionAutomapper Alternative Pin
hoangcute9x3-May-15 18:38
professionalhoangcute9x3-May-15 18:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.