JSON, because it is easy to read and write, but also easy to machine parsing and generation, strong compatibility, has become the current server and Web applications between the accepted standard of data transfer. In this article we ran a benchmark test in the Java programming language to test a few common JSON libraries, and see how fast the library is when parsing files of different sizes.
JSON is typically used to transmit and parse large files. This is a common scenario for data handlers running on Hadoop or spark clusters. Under the given file size, you can see that there is a significant difference in the parsing speed between the different libraries.
In high throughput situations, small files are transmitted and parsed frequently, so the potential performance gap at the outset is not obvious. But if you need to parse a lot of small files frequently under very high loads, the gap starts to grow. Micro-services and distributed architectures often use JSON to transmit such files, because this is already the fact standard of the Web API.
Not all JSON libraries are called "Trent". It is important to choose the correct library based on the usage scenario. Hopefully this benchmark test will help you.
JSON Library
Json.simple vs Gson vs. Jackson vs JSONP
We selected four major JSON libraries for benchmarking: Json.simple, Gson, Jackson and JSONP. JSON parsing in Java typically uses these libraries, and the reason for choosing them is that they appear very frequently in GitHub projects.
Here is the JSON library we tested:
Yidong Fang of Json.simple (Https://github.com/fangyidong/json-simple). Json.simple is a JSON codec-coded Java tool Library. It is designed to create a lightweight and High-performance tool library.
Google's Gson (Https://github.com/google/gson). Gson This Java library is able to convert between Java objects and JSON. It also provides complete support for Java generics and does not require you to add annotations to the class. It is more convenient to use without adding annotations, and is a necessary prerequisite if you cannot modify the source code.
Fasterxml's Jackson Project (Https://github.com/FasterXML/jackson). Jackson is a data processing tool kit, and its highlights are streaming JSON parsers and generators. It is designed for Java and can also handle other non-JSON encodings. From our statistics in GitHub, it should be the most popular JSON parser.
Oracle's JSONP (https://jsonp.java.net/). JSONP (JSON processing) is a set of Java APIs processed by JSON that is used to generate and parse JSON strings from the name. This is an open source implementation of the JSR353 specification.
Benchmark Test
We used both large and small files to benchmark these libraries. As the size of the file varies, the system resources needed to process the text will also rise.
This benchmark focuses on two key scenarios: the resolution speed under large files (190MB) and the resolution speed under Small files (1KB). The large file is taken from here: Https://github.com/zeMirco/sf-city-lots-json. Small files are randomly generated from here: http://www.json-generator.com/.
Whether it's a large file or a small file, we'll run it 10 times with the same library. For each large file, we will use the same library to run 10 times separately. For small files, it is repeated 10,000 times in a single run of a single library. In each iteration of the small file test, the contents of the file do not reside in memory, and the machine running the test is the C3.large instance of AWS.
The full test results for large files are as follows, and I've averaged the results of small files. To see the full results, please visit here. If you want to see the source code of the small file test, please download it from here.
Large file Results
The results are very different! Jackson and json.simple the test, and Jackson was slightly better than json.simple. Judging from the average results of the test run, Jackson and Json.simple in the big file on the performance of some good, and JSONP ranked third behind the far, Gson is the bottom of the distant.
Let's convert the result to a percentage. On average, Jackson had to win. The following is the percentage data for the results, which can be compared from two dimensions:
The performance difference between different libraries is really not small.
Conclusion: Jackson won with a slight advantage. Json.simple followed, while the remaining two were lagging far behind.
Small file Results
The table above records the average time to parse 10 times for each file, and the total average time is shown below. Each library has the following number of wins in a small file test:
GSON-14
JSONP-5
Jackson-1
json.simple-0
The result seems to be convincing. However, judging from the average results of all the documents, Gson the champion is still well-deserved, json.simple and Jsonp two or three of the dispute should be no suspense. Jackson is at the bottom of the wheel. Although Json.simple did not take the first place on any document, overall it was ranked second in terms of parsing speed. Jsonp, despite winning the championship on many documents, averaged only the third place on average.
Also noteworthy is that although Jackson is the slowest library in the round, it behaves very well in all of the files, while the other three libraries are more likely to come by chance than Jackson, but in other files the parsing speed is even worse.
Let's convert these numbers to percentages, or the same two dimensions:
Compared with large file tests, this time the gap is relatively small, but also can not be overlooked.
Conclusion: Unfortunately, json.simple a weak disadvantage with the championship, this round of Gson wins. JSONP is still the millennium third and this time Jackson is catching a late set.
Summarize
Parsing speed is not the only metric to measure a JSON library, but it is really important. By running this benchmark test, we found that none of the libraries could beat the opponents on all the files. The good in large files is rooted in small files, and vice versa.
If you want to choose which library from the resolution speed, it depends on your use of the scene.
If your application often handles large JSON files, then Jackson should be your dish. Gson on the big papers.
If you are primarily dealing with small file requests, such as the initialization of a micro-service or distributed architecture, then Gson is preferred. Jackson's performance on small papers was unsatisfactory.
If you are dealing with both of these files, then the Json.simple, who are second in both rounds of performance, are more suited to such scenarios. Jackson and Gson did not perform well on different file sizes.
Unless the resolution speed is not considered, JSONP is nothing to be commended for. Its performance in large files and small files is bad compared to other libraries. Fortunately, Java 9 will soon have native JSON implemented, I believe JSONP future performance is still worth looking forward to.