Performance Comparison of open-source JSON libraries: JSON. simple/GSON/Jackson/JSONP, jsongson

Source: Internet
Author: User

Performance Comparison of open-source JSON libraries: JSON. simple/GSON/Jackson/JSONP, jsongson
Which JSON library in Java has the fastest parsing speed?

JSON has become a widely accepted standard for data transmission between servers and WEB applications. However, as many of the things we have become accustomed to, you will naturally think about it without further thinking. We seldom think about the differences between these JSON libraries, but they are actually not the same. Therefore, we ran a benchmark test to test several commonly used JSON libraries to see which library is the fastest when parsing files of different sizes. I will share the results below.

JSON is usually used to transmit and parse large files. This is a common scenario for data processing programs running on Hadoop or Spark clusters. Given the file size, you can see that there is a significant difference in the resolution speed between different libraries.

When the throughput is high, small files are frequently transmitted and parsed. Therefore, the performance gap may not be obvious at the beginning. However, if you need to frequently parse a large number of small files under very high loads, the gap will increase. Microservices and distributed architectures often use JSON to transmit such files, because this is already a de facto standard for WEB APIs.

Not all JSON databases are called "trunsu ". It is important to select the correct database based on the Application scenario. We hope this benchmark will be helpful to you.

JSON library JSON. simple vs GSON vs Jackson vs JSONP

We chose four mainstream JSON libraries for Benchmark Testing: JSON. simple, GSON, Jackson, and JSONP. These libraries are usually used for JSON parsing in Java. The reason for choosing them is that they often appear frequently in Github projects.

The JSON library we tested is as follows:

  • Yidong Fang JSON. simple (https://github.com/fangyidong/json-simple ). JSON. simple is a JSON codec 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 Java library can be converted between Java objects and JSON objects. It also provides complete support for Java generics, and you do not need to add annotations on the class. It is more convenient to use without adding annotations, and this is a necessary prerequisite when the source code cannot be modified.

  • FasterXML's Jackson Project (https://github.com/FasterXML/jackson ). Jackson is a tool suite for data processing. It highlights the streaming JSON parser and generator. It is designed for Java and can also process other non-JSON codes. According to 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. It is used to generate and parse JSON strings by name. This is an open-source implementation of the JSR353 specification.

Benchmark Test

We used both large files and small files to benchmark these libraries. As the file size varies, the system resources required to process these texts also increase.

This benchmark mainly focuses on two key scenarios: The resolution speed of large files (190 MB) and that of small files (1 kb. The large file is taken from here: https://github.com/zemirco/~city-lots-json. Small files are randomly generated here: http://www.json-generator.com /.

Whether it is a large file or a small file, we will use the same library to run it again for 10 times. For each large file, we use the same database to run the program for 10 times. For small files, a single running of a single database will repeat 10000 times. In each iteration of the small file test, the file content does not reside in the memory, and the machine running the test is the AWS c3.large instance.

The complete test results of the large file are as follows. I calculate an average value for the results of the small file. To view the complete results, please move here. If you want to see the source code of the small file test, download it from here.

Large file results

The results differ greatly! Jackson and JSON. simple took the lead in this round of tests. On the whole, Jackson is slightly better than JSON. simple. From the results of the test run, Jackson and JSON. simple have better performance in large files, while JSONP ranks third behind, and GSON is far from the bottom.

Then we can convert the result into a percentage. On average, Jackson is better off. The following figure shows the percentage data of the result. You can compare the result in two dimensions:

The performance difference between different databases is not small.

Conclusion: Jackson won with a slight advantage. JSON. simple follows, while the remaining two libraries are far behind.

Small file Result

The above table records the average time for each file to be parsed for 10 times. The total average time is shown below. The number of times each database won the championship in a small file test is as follows:

  • GSON-14
  • JSONP-5
  • Jackson-1
  • JSON. simple-0

This result seems very convincing. However, from the average results of all files, the GSON champion is well-deserved. The two or three competitions between JSON. simple and JSONP should be of no suspense. Jackson is at the bottom of this round. Although JSON. simple has not won the first place in any file, its resolution speed is generally ranked second. Although JSONP won the championship in many documents, it only scored the third place on average.

It is also worth noting that, although Jackson is the slowest database in this round, it performs very consistently in all files, and the other three libraries, although by chance, will be much faster than Jackson, however, the parsing speed on other files is quite or even worse.

Let's convert these numbers into percentages to see if they are in the same two dimensions:

Compared with the large file test, the gap is relatively small, but it cannot be ignored.

Conclusion: Unfortunately, JSON. simple was defeated by a weak weakness and the championship. JSONP is still three years old, and Jackson is catching up late this time.

Summary

The resolution speed is not the only indicator to measure a JSON database, but it is indeed very important. By running this benchmark, we found that no database can beat its opponent on all files. Large files have excellent performance, but the root header is planted on small files, and vice versa.

If you want to determine the database to be selected based on the resolution speed, it depends on your application scenario.

  • If your application often processes large JSON files, Jackson should be your dish. GSON is very hard on large files.
  • If you mainly process small file requests, such as initialization of A microservice or distributed architecture, GSON is the first choice. Jackson is not doing well in small files.
  • If these two types of files are frequently processed, JSON. simple, which ranks second in both the two rounds of performance, is more suitable for such scenarios. Jackson and GSON are not doing well in different file sizes.

Unless resolution speed is not taken into account, JSONP has nothing to praise. Its performance in large files and small files is worse than that in other libraries. Fortunately, Java 9 will soon implement native JSON, and I believe JSONP's future performance is still worth looking forward.

The lecture is finished. If you are sensitive to the parsing speed of the JSON library, select Jackson for large files, GSON for small files, and JSON. simple for both. If you have any questions about this benchmark, please leave a message below.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.