Which is faster: Automapper, Valuinjector, or manual mapping? To what degree is each one faster? [closed]
Asked Answered
P

1

19

Suppose I have this object in my DAL (ORM etc)

public class Student
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}
   public Parent Parent {get;set;}
}
public class Parent
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}
}

And I have a ViewModel that looks like this

public class StudentDetailVM
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}

   public string ParentName {get;set;}
   public string ParentPhone {get;set;}
}

In that case I need to flatten the objects. I can do this with a tool like Automapper, ValueInjector, or I could do it manually. This is tedious work if there are many such classes to handle, but there appears to be a performance / developer efficiency tradeoff between all three approaches.

I'm looking for guidance on when to use Automapper vs Valueinjector vs a manual mapping. I'm sure manual mapping is the fastest one, but by how much?

  1. Are some scenarios much slower/faster than others (e.g. flattening, etc)?

  2. Would it make sense to do a hybrid approach to mapping objects between layers?

The reason I ask is because a Codeplex project called emitmapper was created to address performance issues in automapper, and I remember seeing a comment that said automapper may take up to .5ms to map a large class. (reference needed)

I also remember seeing an article that describes how users have a higher chance of staying on your site if it loads within 70ms, as opposed to 90ms or more. (I'm looking for this link too). If automapper is consuming most of my page-load time, combined with network latency, then I see potential to not use automapper and create manual classes for my high volume pages and stick with a hybrid approach.

Bottom line: I would run the tests myself, but I don't know enough about .NET internals to create accurate results that can be used as a reusable guideline.

Pentane answered 14/11, 2011 at 13:33 Comment(5)
I suppose that very much depends on what you're actually trying to do in the mappings. It would be a pretty simple scenario for you to test though, for your requirements. Recreate a simple requirement for you 3 ways and compare the speed. I like Automapper, but I don't use it for everything.Paleface
Will the downvoters kindly add constructive feedback? I've made 4 revisions to this and it appears to be limited in scope (not overly broad), constructive (somebody favorited it).Pentane
IMHO the real question is "are you sure you want to break the type system over this?"Sussex
Take a look at this benchmark: expressmapper.org/#benchmarksToluol
Typical old guard SO scenario. Someone wants to know generally what the performance tradeoff of each method is, and they're only told why knowing that isn't important. The votes on this post and its "answer" are a good indication of what people are asking for vs. what information is being provided.Emlyn
U
4

Bottom line: I would run the tests myself, but I don't know enough about .NET internals to create accurate results that can be used as a reusable guideline.

You don't need to know .NET internals. You just need to know what your performance requirements are and what your typical usage is going to look like. Profile the code under a typical usage scenario in the all the variety of ways, and choose that which meets your performance requirements and is easiest to maintain (i.e., don't necessarily choose the most performant; there are other criteria).

Urology answered 14/11, 2011 at 14:50 Comment(3)
Thank you for your response. Should I accept the fact that it's not possible or beneficial to have a performance baseline between the 3 approaches because there is a huge variance in the results?Pentane
Yes, performance will likely vary depending on usage. What matters is the relative performance for your specific use case and performance requirements.Urology
I see, so I think I can assume consistent high performance with manual mapping. Anything else millage may vary, and in some cases, significantly.Pentane

© 2022 - 2024 — McMap. All rights reserved.