I have downloaded and testing these two mapping libraries. I wrote a program which has 100000 iterations and maps the beans of the same class:
public class IntBean {
@JMap
private int int1;
@JMap
private int int2;
.
.
.
@JMap
private int int10;
}
Mappers are created BEFORE iterations start:
private JMapper jmapper = new JMapper(IntBean.class, IntBean.class);
private MapperFactory orikaFactory = new DefaultMapperFactory.Builder().build();
private MapperFacade orikaFacade = null;
orikaFactory.registerClassMap(orikaFactory.classMap(IntBean.class,IntBean.class).byDefault().toClassMap());
orikaFacade = orikaFactory.getMapperFacade();
What is in each iteration:
this.orikaFacade.map(a1, a2);
or
a2 = (A) this.jmapper2.getDestination(a1);
Hand mapping: 1ms
Orika mapping: 32ms
Hand mapping: 6ms GREAT SPEED !!!
Dozer: 1140ms
I know, that Orika and Jmapper are great libraries from Google and they use reflection in a different way than for example Dozer, which is much slower, they se reflection to generete code somehow..
I have 3 questions:
1) How they work - when the code is generated, during maven build, in runtime - everytime when I create mapper in code? Are they change class code byte dynamically when I create mappers.?
2) Why there is this speed difference that I noticed? If the generate code somehow, then why there are different results
3) Which library would you choose and why? Both have the same capabilities? Why both come from Google? Why Google didnt develop Orika and created Jmapper instead?
I'm not familiar with Jmapper, so i'll concentrate on Orika and Dozer
How do they work? They both work reasonably differently. Dozer using reflection and Orika using bytecode generation. During maven build? Nothing happens, its all done at runtime. Dozer access fields via their get methods and sets the value in a target object via setter methods. Orkia generates bytecode to do the work as if you'd done a hand mapping yourself. Its slow on the first conversion and should be faster on each one after that.
Dozer, should always be roughly the same speed, relies on reflection. Orika, bytecode generation, 1st run should be alot slower while its generating the mapping code.
Short answer, it depends. What are you trying to map? Dozer is very good at mapping from one type to another if the classes are roughly similar. It doesnt deal with Maps at all. Be prepared to write custom converter code if you have a Map in your Object
Orika is fantastic at mapping data between two objects of the same type. Some of its List handling is a little odd where it'll treat a list like a single object instead of a collection of individual objects. Again, be prepared to write some code for this too.
Neither handles a large Object graph particularly well, be prepared to write a lot of configuration for this one.
Unless you're going to be doing a lot of mapping in an application, or mapping that needs to change frequently. Write your own