JSON has become a recognized standard for data transfer between the current server and the Web application, but just as many of the things we are accustomed to, you will feel that it is taken for granted that you do not think deeply. We rarely think about how these JSON libraries are different, but in fact they are really not the same. Therefore, we ran a benchmark to test several commonly used JSON libraries to see which library was the fastest when parsing files of different sizes. I'll share the results with you.
JSON is typically used to transfer and parse large files. This is a common scenario for data handlers running on Hadoop or in a spark cluster. With a given file size, you can see that there is a significant difference in the resolution speed between the different libraries.
In high-throughput situations, small files are frequently transmitted and parsed, so the performance gap may not be obvious at first. But if you need to parse large numbers of small files frequently under very high loads, the gap will start to grow. MicroServices and distributed architectures often use JSON to transfer such files, as this is already the de facto standard for Web APIs.
Not all JSON libraries are called "Clenbuterol". It is important to choose the correct library based on the usage scenario. Hopefully this benchmark will help you.
json.simple vs Gson vs Jackson vs JSONP
We have selected four main JSON libraries for benchmarking: Json.simple, Gson, Jackson and JSONP. JSON parsing in Java usually uses these libraries, because they are very frequent in the GitHub project.
Here is the JSON library we tested:
Yidong Fang's Json.simple (https://github.com/fangyidong/json-simple). Json.simple is a JSON-encoded Java tool Library. It is designed to create a lightweight, simple and high-performance tool library.
Google's Gson (Https://github.com/google/gson). Gson This Java library can be converted between Java objects and JSON. It also provides full support for Java generics and does not require you to add annotations to the class. It's easier to use without adding annotations, and it's a necessary prerequisite if you can't modify the source code.
Fasterxml's Jackson Project (Https://github.com/FasterXML/jackson). Jackson is a tool suite for data processing, and its highlights are streaming JSON parsers and generators. It is designed for Java and can handle other non-JSON encodings as well. Judging from our statistics on 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, which is used to generate and parse JSON strings from the name. This is an open source implementation of the JSR353 specification.
Benchmark Test
We benchmark these libraries with large files and small files at the same time. As the file size is different, the system resources needed to process the text will also rise.
This benchmark focuses on two key scenarios: parsing speed under Large files (190MB) and parsing speed under small files (1KB). Large files are 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 a small file test, the contents of the file do not reside in memory, and the machine running is the C3.large instance of AWS.
The full test results for large files are as follows, and I have averaged the results of small files. To see the full results, please go here. If you want to see the source of the small file test, please download it from here.
Large file Results
The results vary greatly! Jackson and Json.simple lead this round of tests, and overall, Jackson is 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 excellent, and JSONP ranked third behind very far, Gson is a distant bottom.
Let's convert the results into percentages. Jackson was on average to win. The following is the percentage data for the result, which can be compared from two dimensions:
The performance difference between the different libraries is really not small.
Conclusion: Jackson wins with a slight advantage. Json.simple followed, while the remaining two libraries lagged far behind.
Small file Results
The above table records the average time to parse 10 times for each file, and the total average time is shown below. The number of times each library won in a small file test is as follows:
- 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 this champion is well deserved, json.simple and Jsonp's two or three-man dispute should be no suspense. Jackson was at the bottom of the wheel. Although Json.simple did not win the first place on any file, it was ranked second in overall terms. While Jsonp won the championship on many documents, he only got the third place on average.
It is also worth noting that although Jackson is the slowest library in the world, it is very consistent across all files, and the other three libraries, though by chance, are much faster than Jackson, but on other files the parsing speed is even worse.
Let's turn these numbers into percentages and see the same two dimensions:
Compared with the big file test, this time the gap is relatively smaller, but also can not be ignored.
Conclusion: Unfortunately, Json.simple again with a weak disadvantage and the championship missed, this round Gson win. JSONP is still millennium old three and this time Jackson drove a late episode.
Summary
Parsing speed is not the only metric for measuring a JSON library, but it is really important. By running this benchmark, we found that no library could beat opponents on all files. A large file is good but it has a root on a small file and vice versa.
If you want to choose which library from the resolution speed, it depends on your usage scenario.
- If your app often handles large JSON files, then Jackson should be your dish. The Gson on the large file was rather labored.
- If you are primarily dealing with small file requests, such as the initialization of a microservices or distributed architecture, then Gson is preferred. Jackson's performance on small papers was less than satisfactory.
- If you are always dealing with both of these files, then the second json.simple in both rounds is more appropriate for such scenarios. Jackson and Gson did not perform well on different file sizes.
Unless the parsing speed is not considered, JSONP is nothing to be commended for. It behaves poorly on large files and small files compared to other libraries. Fortunately, Java 9 will soon have its native JSON implementation, believing that Jsonp's future performance is still worth looking forward to.
Finally finished. If you are sensitive to the parsing speed of the JSON library, large files choose Jackson, small files Choose Gson, and both are json.simple. If you have any questions about this benchmark test, please leave a comment below.
JSON Learning Summary (2) Performance comparison of JSON libraries under--java: Json.simple vs. Gson vs. Jackson vs. JSONP