Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Parsing performance comparison of open source JSON libraries (Jackson / Json.simple / Gson)

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Json has become a recognized standard for data transmission between servers and web applications.

Microservices and distributed architectures often use Json to transfer such files because this is already the de facto standard of webAPI.

But like many things we are used to, you will take it for granted and stop making further comparisons.

We rarely think about the differences between the Json libraries we use, but in fact they are different.

So our team ran tests on three commonly used Json libraries to see which library is the fastest when parsing files of different sizes.

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, parsed small files are transferred frequently, 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.

Not all Json libraries are called "Trensu". How to choose the right library according to the usage scenario is very important.

Back to the top.

1. Selected open source Json library

Select three mainstream Json libraries for benchmarking: Jackson, Json. Simple, GSON

Json.simple of Yidong Fang (https://github.com/fangyidong/Json-simple). Json.simple is a Java tool library for Json codec. It aims to create a lightweight, simple and high-performance tool library.

Gson (https://github.com/google/gson)) of 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 (https://github.com/FasterXML/jackson). 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.

Back to the top.

two。 Benchmark testing under relative conditions

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:

1. The parsing speed of large files (190MB) and small files (1KB). Large files are taken from here: https://github.com/zeMirco/sf-city-lots-Json.

two。 The small files are randomly generated from here: http://www.Json-generator.com/.

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.

The contents of the file do not reside in memory during each iteration of the small file test.

Large file result

Jackson and Json. Simple took the lead in 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 perform better on large files.

JsonNP lags far behind in the third place, and Gson is far from the bottom.

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

Json P-5

Jackson-1

Json .simple-0

Gson is a well-deserved champion, and there is no suspense about the second place in Json.simple. Jackson is at the bottom of this round.

Although Json.simple did not win the first place in any file, overall it ranks second in parsing speed.

It is also worth noting that although Jackson is the slowest library in this round, it behaves very consistently in all files.

Although the other three libraries happen to be much faster than Jackson, they parse equally or even worse on other files.

Back to the top.

3. Finally, a 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 the first choice. 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.

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.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report