Friday, July 20, 2012

Gson vs Jackson - which library is faster?

I've been reading about how jackson is the fastest json parser.  And I wanted to know what exactly that meant.

I've used gson in the past and have never had a problem wth the speed of it, but I've never been in a situation where every bit of speed counts.  

I devised a simple test.  Using junit.

Test 1:
Serialize the same object 100 times with each gson and jackson and time how long it takes.

Results:
gson is between 150 and 200 ms for my object every time.
jackson is between 670 and 720 ms for my object every time.

Clearly here gson is the winner.  However, I wanted to make sure I didn't sell jackson short, maybe it excels in another area.

Test 2:
Serialize both with gson and jackson in the same unit test with gson first in case object creation matters.

Results:
gson is between 150 and 200 ms every time.
jackson is between 620 and 680 ms all but one time which was 901 ms.


Test 3:

Serialize both with gson and jackson in the same unit test with jackson first in case object creation matters.

Results:
gson is between 120and 170 ms every time.
jackson is between 589 and 700 ms every time.



Finally I decided if I'm using it in a web service I'll need to create POJOs from the JSON for processing if there is a post.  

Test 4:
Build POJO from JSON

Result:
gson is between 103 and 141 ms every time.
jackson is between 469 and 546 ms every time.

An interesting thing I found is that the object I was dealing with had a Calendar in it.  This serializes and deserializes just fine in jackson, but in gson it would create a nice output, but did not populate the POJO correctly.  When I changed it to a Date object both Jackson and gson worked correctly.

My final review is that clearly gson is faster than Jackson.  In it's worst case and Jackson's best case gson is still always twice as fast.   I had used gson, just because I found it easy to use, but now that I have run some benchmarks I know I happened to get lucky enough to find the faster library.

4 comments:

Anonymous said...

That's a nice and simple way of benchmarking, although unit tests aren't really necessary. Have you tried doing this with a billion POJOs? Might see an interesting result.

Anonymous said...

lol this is not a fair comparison. You should attach the source code of your test application.
I really suspect that you're misusing jackson.
For example, are you sure you're not making a silly mistake like creating a new ObjectMapper for each serialization/deserialization?
Creating an ObjectMapper is really time consuming but once configured it's thread-safe and really fast.

will.tran said...

^^^^ What that guy said. These results are useless without some peer review, which means posting your code.

Anonymous said...

One more comment, beyond "please show the source": one thing you absolutely MUST do is to warm up the VM. You should never take first N iterations as measurement, but run the test for certain number of times (or number of seconds); and then measure stable throughput.

Not doing this gives arbitrary results.