Automapper vs Mapster

AutoMapper is undoubtedly the most popular object to object mapping library used around and it wouldn’t be a surprise to know if any of us, including yours truly, have even tried out alternatives. I recently tried out Mapster and was quite surprised by the results. Even they did mention their Github pages about being faster than Automapper, I honestly didn’t believe the performance difference until I saw it myself. And truly, I am impressed.

I ran a couple of tests with the following models.

public class SimpleObjectSource
{

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Department { get; set; }
}

public class SimpleObjectDestination
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Department { get; set; }
}

public class ComplexObjectSource
{
    public int Id { get; set; }

    public IEnumerable<int> Values { get; set; }

    public List<ComplexObjectSource> ComplexCollection { get; set; }
}

public class ComplexObjectDestination
{
    public int Id { get; set; }

    public IEnumerable<int> Values { get; set; }

    public List<ComplexObjectDestination> ComplexCollection { get; set; }
}

With the models defined above, I create a few Benchmark tests which could compare the mapping performance when mapping individual entity as well as when mapping collections of the entities. Following were the Benchmark Results I got when mapping single entities.

|                                   Method |          Mean |        Error |       StdDev |        Median |
|----------------------------------------- |--------------:|-------------:|-------------:|--------------:|
|  SingleEntitySimpleObjectUsingAutomapper |     162.53 ns |    14.191 ns |    40.027 ns |     141.16 ns |
|     SingleEntitySimpleObjectUsingMapster |      45.40 ns |     0.907 ns |     1.726 ns |      45.15 ns |
| SingleEntityComplexObjectUsingAutomapper | 152,409.98 ns | 2,966.145 ns | 2,913.151 ns | 151,781.99 ns |
|    SingleEntityComplexObjectUsingMapster |   5,290.26 ns |    76.098 ns |    63.545 ns |   5,298.70 ns |

While following were the results when mapping collections

|                           Method |          Mean |        Error |        StdDev |
|--------------------------------- |--------------:|-------------:|--------------:|
|  SimpleCollectionUsingAutomapper |      24.00 us |     0.472 us |      0.614 us |
|     SimpleCollectionUsingMapster |      18.60 us |     0.499 us |      1.456 us |
| ComplexCollectionUsingAutomapper | 343,087.60 us | 6,768.516 us | 15,277.668 us |
|    ComplexCollectionUsingMapster |  45,739.96 us |   905.842 us |  2,479.728 us |

As one can observe there are huge performance benefits with the Mapster implementation. I am not quite sure if this is a fair comparison though, as these tests only compare the performance on certain conditions, and do not consider the overall functionality provided by the library. Maybe I will compare the functionality on a separate blog. But for the performance under the particular conditions, I am pretty impressed by Mapster even though I had been using AutoMapper whole my life for mapping.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s