Rick

Rick
Rick

Friday, April 18, 2014

Which is faster Java Object Serialization (ObjectOutputStream/ObjectInputStream) or Jackson JSON serialization? The answer will surprise you.

Most people assume that Java object serialization is faster than Jackson JSON serialization because Jackson is using JSON and Java object serialization is binary. But most people are wrong.
Jackson JSON serialization is much faster than built in Java object serialization.
Boon JSON serialization is much faster than Jackson JSON serialization.

There are serveral tests. There is a one that serializes the data. One test serializes and then reads. Then there are two types of Object graphs. One is small and one is quite beefy.

We run the benchmarks with JMH (info on JMH below). The full source for the benchmark code is on github.


java -jar target/microbenchmarks.jar ".*serialization.*" -wi 2 -i 3 -f 2 -t 16 



Boon 14 

Testing the Jackson and Boon JSON serializers vs. Java Object serialization.
i.g.j.s.MainBoonSerializer.roundTripBig              thrpt  16         6    1      101.708       11.041    ops/s
i.g.j.s.MainJavaSerialization.roundTripBig           thrpt  16         6    1       91.339        4.354    ops/s
i.g.j.s.MainJacksonSerializer.roundTripBig           thrpt  16         6    1       76.406        2.671    ops/s
Boon wins by 30%. Jackson comes in third. Boon beats Java binary serialization. :)
i.g.j.s.MainBoonSerializer.serializeBig              thrpt  16         6    1      377.694       20.544    ops/s
i.g.j.s.MainJavaSerialization.serializeBig           thrpt  16         6    1      174.208       23.116    ops/s
i.g.j.s.MainJacksonSerializer.serializeBig           thrpt  16         6    1      186.008       16.769    ops/s
Boon wins by 2x.
i.g.j.s.MainBoonSerializer.roundTriper               thrpt  16         6    1   312390.819    57581.454    ops/s
i.g.j.s.MainJacksonSerializer.roundTriper            thrpt  16         6    1   218978.594    44249.526    ops/s
i.g.j.s.MainJavaSerialization.roundTriper            thrpt  16         6    1    47756.764    59303.464    ops/s
Both Boon and Jackson kill Java serialization. Boon also kills Jackson serialization round trip.
i.g.j.s.MainBoonSerializer.serializeSmall            thrpt  16         6    1   936352.708   137569.222    ops/s
i.g.j.s.MainJacksonSerializer.serializeSmall         thrpt  16         6    1   636723.378    31346.173    ops/s
i.g.j.s.MainJavaSerialization.serializeSmall         thrpt  16         6    1   382283.631    25397.336    ops/s
Boon wins by a 30% margin. Jackson comes in second. Both demolish Java built-in serialization.
I recently added some optimizations to Boon JSON serialization 15 (still in snapshot). Let's see what it does to performance.
Benchmark                                        Mode Thr     Count  Sec         Mean   Mean error    Units
i.g.j.s.MainBoonSerializer.serializeSmall       thrpt  16         6    1   928002.808    60950.051    ops/s
i.g.j.s.MainJacksonSerializer.serializeSmall    thrpt  16         6    1   611743.833    79255.225    ops/s
i.g.j.s.MainJavaSerialization.serializeSmall    thrpt  16         6    1   379509.836    14233.369    ops/s
About the same.
i.g.j.s.MainBoonSerializer.roundTripBig         thrpt  16         6    1       96.589       34.860    ops/s
i.g.j.s.MainJavaSerialization.roundTripBig      thrpt  16         6    1       87.097        8.706    ops/s
i.g.j.s.MainJacksonSerializer.roundTripBig      thrpt  16         6    1       78.378        9.286    ops/s
About the same.
i.g.j.s.MainBoonSerializer.roundTriper          thrpt  16         6    1   303601.772    67861.688    ops/s
i.g.j.s.MainJacksonSerializer.roundTriper       thrpt  16         6    1   206512.825     9533.699    ops/s
i.g.j.s.MainJavaSerialization.roundTriper       thrpt  16         6    1    42197.833    60428.583    ops/s
About the same.
i.g.j.s.MainBoonSerializer.serializeBig         thrpt  16         6    1      356.867       28.637    ops/s
i.g.j.s.MainJacksonSerializer.serializeBig      thrpt  16         6    1      173.494       24.818    ops/s
i.g.j.s.MainJavaSerialization.serializeBig      thrpt  16         6    1      163.283       23.345    ops/s
About the same as before.
In short. Jackson is very fast. Boon is faster. And my latest rounds of optimizations did nothing. :(

Have you heard of Boon JSON parser? Have you heard of Boon?

Groovy and Boon provide the fastest JSON parser for the JVM | Java JSON Benchmarks for Jackson vs. Boon/Groovy

Boon Home | Boon Source | If you are new to boon, you might want to start here. Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity. A real boon to Java to developers!

Boon and Groovy 2.3 provide the fastest JSON parser on the JVM. 


Jackson is consistently faster than GSON and JSONSmart.
The Boon JSON parser and the new Groovy 2.3 JSON parser are faster than Jackson.



Groovy JSON support and the Boon JSON parser are up to 3x to 5x faster than Jackson at parsing JSON from String and char[], and 2x to 4x faster at parsing byte[].

Groovy JSON support and Boon JSON support are also faster than Jackson at encoding JSON strings. Boon is faster than Jackson at serializing/de-serializing Java instances to/fro JSON. The core of the Boon JSON parser has been forked into Groovy 2.3 (now in Beta). In the process Boon JSON support was improved and further enhanced. Groovy and Boon JSON parsers speeds are equivalent. Groovy now has the fastest JSON parser on the JVM. 

The new Groovy JSON parser based on Boon JSON parser is 20x faster than the previous Groovy JSON parser circa Groovy 2.2.

If you are using Groovy, just upgrade to Groovy 2.3 and should see the speedup. If you are using Java and you can't or wont use Groovy then follow this guide 10 minutes guide to using Boon JSON support (Boon is a Java lib). You can also use Boon with Groovy or Scala or ... :)

See the full benchmarks at Boon JSON Parser versus Jackson with source code.







Java Boon/Groovy vs Jackson - JSON Benchmarks

Boon comes with a very fast JSON parser which has now been ported / forked into Groovy 2.3.

JSON Benchmarks

The benchmarks were done with OpenJDK, and we used sample JSON. 
We used JMH which is what OpenJDK uses. We used the json files from json.org for parsing.


Summary of Results

So maybe you heard the JSONSmart was faster than Jackson. Well you heard wrong. 
The JSONSmart benchmarks were seriously flawed and did not allow for any sort of warmup. 
So in effect for all practical purposes, they are useless.
When we tested Jackson against JSONSmart and GSON, for the most part (like 95% of the time) Jackson always wins. 
Jackson is consistently faster than GSON and JSONSmart. No DOUBT!

But Boon is consistently faster than Jackson and sometimes a lot faster.
Boon JSON parser is faster than Jackson with Readerreading filesbyte[], and char[] and String.
Boon is 3x to 5x faster than Jackson at parsing JSON from String and char[], and 2x to 4x faster at parsing byte[].
Boon and Jackson speeds are much closer with InputStream based parsing. Boon is usually faster at InputStream, but the margins are much smaller.
Boon and Jackson are close at handling InputStream if the JSON stream is small. Once the JSON stream gets over 1KB to 2KB, Boon wins consistently.
Boon is faster at encoding JSON strings, and serializing/de-serializing Java instances to/from JSON than Jackson.

Boon is more than a JSON parser. Learn more about the JSON Boon benchmarks here:



For the non-technical out there who might stumble on this blog post and ask a silly question like is this important.


So let's say your neighbor bought a new Corvette and he called that Corvette Jackson...

Jackson JSON Parser



Hey that is very nice, and fast!

Well you just bought a jet engine named Boon, and rocket ship named Groovy. So while he is doing 140 MPH, you are breaking the sound barrier! Of course now all of your neighbors hate you, but you own a space ship and a rocket so who cares!

Groovy JSON Parser



Boon JSON Parser




The new Groovy JSON parser 2.3 based on Boon JSON parser is 20x faster than the previous Groovy JSON parser circa Groovy 2.2. So Groovy JSON support was like riding a fast horse, but now it is a fighter jet.

Groovy JSON Parser 2.2




Groovy JSON Parser 2.3




Boon JSON and Groovy JSON Benchmarks.

(Maybe not the best choice for horse, but when I think of horse riding, I think of the toughest leader who can probably beat up Chuck Norris riding shirtless no political affiliation or social commentary implied whatsoever.)

Learn more about Boon here:

Looking for a tutorial to get started with Boon?


This five minute guide will get you up to speed using Boon in no time. Parse and serialize Java objects to/fro JSON up to 5x faster! Boon is not a JSON parsing project. It is more than that, but JSON parsing is intrinsic to what Boon is all about. Boon is the fastest way to serialize and parse JSON in Java so far. It is faster at object serialization, enabling JSON expressions, JSON parsing and much more. Boon JSON is FAST! In addition it has a very easy to use, convention-based API. This getting started guide covers making JSON REST calls, JSON object serialization, using annotations, and more.

Need to do ETL with JSON, and/or want to search sort JSON or Java graphs quickly.


Java Boon - Boon Data Repo using Indexed Collections for Java Beans, JSON and Maps like JDK 8 streams but faster. Many languages have sorting, slicing, dicing capabilities for Lists and Maps, and JSON. Boon adds this to Java. Boon Data Repo is an in-memory query engine that can use high speed indexed collections for very fast in-memory queries. This brief tutorial will show you how to use Boon to query JSON, Java objects and Java maps. And also how to do ELT transforms with JSON, Java objects and Java Maps.

Java Boon filtering for Java Beans, JSON and Java Maps like JDK 8 streams but much faster. Many languages have support for querying objects and filtering objects easily. Java does in JDK 8, but Boon adds it as well, and it can compliment features of JDK 8 as well as work in Java 7 land. Boon adds filtering to to Java. You can filter JSON, Java, and Maps. You can also use indexed queries which are a lot faster than linear search queries that come with JDK 8.


Many languages have path expressions. Boon adds this to Java. Boon has powerful path expressions that work with objects, lists and maps. It also works with JSON. Full examples of Java, Maps/Lists and JSON are included in this tutorial.

Just curious about what Boon is...
Boon comes with helper methods that allow you to easily read files, create lists, sets, maps, concurrent maps, sorted maps, sorted sets, etc. It provides slice notation, searching, easy IO (files, http, etc.). The helper methods are safeList, list, set, sortedSet, safeSet,safeSortedSet, etc. The idea is to make Java feel more like list and maps are built-in types.

What if Java collections and Java hierarchies were easily searchable? They are with Boon! What if it were easy to query a complex set of Java objects at runtime? What if there were an API that kept your object indexes (really just TreeMaps, and HashMaps) in sync.? Well then you would have Boon's data repo. This article shows how to use Boon's data repo utilities to query Java objects. Boon comes with a simple criteria API that makes working with Java collections a snap.

Boon has many utility methods to convert objects into maps, lists, JSON, etc. It also has many collection utility methods. One such utility method is deepCopy, which will deep copy a list or set. Programmer as a way to clone Collection e.g. List, Set, ArrayList, HashSet or any other implementation. Or they write a lot of boiler plate code and spend a lot of time doing things to copy lists and sets. With Boon you can do deep copies without messing around with clone.

Boon gets some inspiration from Groovy and Python when dealing with maps, lists and JSON. It addition to adding slice notation and such, which I covered before. Boon has first class support for converting between Maps, List, JSON and Java objects. Boon gets some inspiration from Ruby, Groovy, Python and Perl and aims to make Java programming just a bit more fun.



Why Boon?

Easily read in files into lines or a giant string with one method call. Works with files, URLs, class-path, etc. Boon IO support will surprise you how easy it is. Boon has Slice notation for dealing with Strings, Lists, primitive arrays, Tree Maps, etc. If you are from Groovy land, Ruby land, Python land, or whatever land, and you have to use Java then Boon might give you some relief from API bloat. If you are like me, and you like to use Java, then Boon is for you too. Boon lets Java be Java, but adds the missing productive APIs from Python, Ruby, and Groovy. Boon may not be Ruby or Groovy, but its a real Boon to Java development.

Core Boon Philosophy

Core Boon will never have any dependencies. It will always be able to run as a single jar. This is not just NIH, but it is partly. My view of what Java needs is more inline with what Python, Ruby and Groovy provide. Boon is an addition on top of the JVM to make up the difference between the harder to use APIs that come with Java and the types of utilities that are built into Ruby, Python, PHP, Groovy etc. Boon is a Java centric view of those libs. The vision of Boon and the current implementation is really far apart.

No comments:

Post a Comment

Kafka and Cassandra support, training for AWS EC2 Cassandra 3.0 Training