In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "performance comparison of the JSON library", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn the "performance comparison of the JSON library"!
Which JSON library in Java has the fastest parsing speed?
JSON has become the accepted standard for data transfer between servers and WEB applications, but like many things we are used to, you will take it for granted and stop thinking about it. We rarely think about the differences between the JSON libraries we use, but in fact they are different. So we ran a benchmark to test several commonly used JSON libraries to see which library is the fastest when parsing files of different sizes. Now I will share the results with you.
JSON is usually used to transfer and parse large files. This is a common scenario for data handlers running on Hadoop or Spark clusters. Given the file size, you can see that there is a significant difference in parsing speed between different libraries.
In the case of high throughput, small files are frequently transferred and parsed, so the possible performance gap at the beginning is not obvious. But if you need to parse a large number of small files frequently under a very high load, the gap begins to widen. Microservices and distributed architectures often use JSON to transfer such files because this is already the de facto standard of WEB API.
Not all JSON libraries are called "Trensu". How to choose the right library according to the usage scenario is very important. I hope this benchmark will help you.
JSON library
JSON.simple vs GSON vs Jackson vs JSONP
We chose four mainstream JSON libraries for benchmarking: JSON.simple, GSON, Jackson, and JSONP. These libraries are commonly used for JSON parsing in Java, and they were chosen because of their high frequency of appearances in Github projects.
Here is the JSON library we tested:
JSON.simple for Yidong Fang. JSON.simple is a Java tool library for JSON codec. It aims to create a lightweight, simple and high-performance tool library.
GSON for Google. GSON, a Java library, can convert between Java objects and JSON. It also provides full support for Java generics, and there is no need for you to add comments to the class. It is easier to use without adding annotations, and it is a necessary prerequisite if the source code cannot be modified.
FasterXML's Jackson project. Jackson is a suite of data processing tools. Its highlights are streaming JSON parsers and generators. It is designed for Java and can also handle other non-JSON codes. From our statistics in Github, it should be a * * JSON parser.
JSONP for Oracle. JSONP (JSON Processing) is a set of Java API processed by JSON. From the name point of view, it is used to generate and parse JSON strings. This is an open source implementation of the JSR353 specification.
Benchmark test
We benchmark these libraries with both large and small files. As the file size varies, so does the system resources required to process the text.
This benchmark focuses on two key scenarios: the parsing speed under large files (190MB) and the parsing speed under small files (1KB). Large files are taken from here. The small files are randomly generated from here.
Whether it is a large file or a small file, we will run it repeatedly with the same library 10 times. For each large file, we will run it 10 times with the same library. For small files, it is repeated 10000 times in a single run of a single library. In each iteration of the small file test, the file content does not reside in memory, and the machine on which the test runs is a c3.large instance of AWS.
The complete test results of the large files are as follows, and I averaged the results of the small files.
Large file result
The results are very different! Jackson and JSON.simple took the lead in this round of tests, and overall, Jackson is slightly better than JSON.simple. From the average results of the test run, Jackson and JSON.simple perform better on large files, while JSONP lags far behind in the third place, and GSON is far from the bottom.
Let's convert the results into percentages. On average, Jackson is better than others. The following is the percentage data of the results, which can be compared from two dimensions:
There is a big difference in performance between different libraries.
Conclusion: Jackson wins with a slight advantage. JSON.simple is not far behind, while the remaining two libraries lag far behind.
Small file result
The above table records the average time of 10 parses for each file, and the total average time is shown below. The number of times that each library won the championship in the small file test is as follows:
GSON-14
JSONP-5
Jackson-1
JSON.simple-0
The result seems to be very persuasive. However, judging from the average results of all the documents, GSON is a well-deserved champion, and there should be no suspense about the battle between JSON.simple and JSONP for the second or third place. Jackson is at the bottom of this round. Although JSON.simple didn't win any files, it ranks second in overall parsing speed. Although JSONP won the championship in many documents, he only finished third on average.
It is also worth noting that although Jackson is the slowest library in this round, its performance is very consistent in all files. Although the other three libraries are occasionally much faster than Jackson, the parsing speed on other files is equal or even worse.
Let's convert these numbers into percentages and see if they are the same two dimensions:
Compared with the large file test, the gap this time is relatively small, but it can not be ignored.
Conclusion: unfortunately, JSON.simple missed the championship again by a slight disadvantage, and this round of GSON won. JSONP is still the third in the millennium, but this time Jackson caught up with a late episode.
Summary
Parsing speed is not the only measure of a JSON library, but it is really important. By running this benchmark, we found that no library could beat its rivals on all the files. Those who perform well in large documents are rooted in small ones, and vice versa.
Which library to choose in terms of parsing speed also depends on your usage scenario.
If your app often handles large JSON files, then Jackson should be your type. GSON works very hard on large files.
If you are mainly dealing with small file requests, such as the initialization of a microservice or distributed architecture, then GSON should be *. Jackson's performance on small files is not satisfactory.
If you often deal with both files, then JSON.simple, which ranks second in both rounds of performance, is more suitable for such scenarios. Jackson and GSON do not perform very well in different file sizes.
Unless parsing speed is not taken into account, JSONP has nothing to praise at all. It performs poorly on both large and small files compared to other libraries. Fortunately, Java 9 will have a native JSON implementation soon, and I believe the future performance of JSONP is still something to look forward to.
Thank you for reading, the above is the content of "performance comparison of JSON library", after the study of this article, I believe you have a deeper understanding of the performance comparison of JSON library, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.