avatarTobias Streng

Summary

The article compares the performance of two JSON frameworks for .NET, Newtonsoft's Json.NET and Microsoft's System.Text.Json, under .NET 7, showing that System.Text.Json outperforms Json.NET in serialization and deserialization tasks.

Abstract

In the context of .NET performance, the article scrutinizes the serialization and deserialization capabilities of the two leading JSON frameworks for .NET: Newtonsoft's Json.NET and Microsoft's System.Text.Json. Utilizing the latest versions of each framework and employing realistic data scenarios, the author conducts benchmarks to assess performance differences. The results indicate that System.Text.Json, provided by default with the .NET SDK since .NET Core 3.1, significantly surpasses Json.NET in speed and memory efficiency across various use cases, including serializing and deserializing both large and small datasets. The author emphasizes the importance of these findings for developers aiming to optimize their .NET 7 applications, suggesting that System.Text.Json should be the preferred choice for JSON handling based on the performance metrics presented.

Opinions

  • The author suggests that Microsoft's System.Text.Json is not only faster but also more memory-efficient than Newtonsoft's Json.NET for JSON serialization and deserialization tasks in .NET 7.
  • It is implied that Newtonsoft's claim of high performance being "faster than .NET’s built-in JSON serializers" may no longer hold true with the advancements in System.Text.Json as of .NET 7.
  • The article conveys that the performance gains seen with System.Text.Json are substantial, with it being nearly twice as fast and using significantly less memory in certain scenarios compared to Json.NET.
  • The author advises that the performance benefits of System.Text.Json could be extrapolated to other scenarios beyond those tested, suggesting it as the generally superior choice for JSON serialization and deserialization in .NET 7 applications.
  • The author provides a caveat that the performance comparison is specific to .NET 7 and that results may vary in other versions of the .NET framework.
  • The author encourages readers to follow their work for more insights on clean architecture, coding, and tech stacks related to C#, .NET, and Angular, indicating a commitment to providing educational content in these areas.

.NET Performance #2: Newtonsoft vs. System.Text.Json

Did Microsoft finally catch up?

Photo by Glenn Carstens-Peters on Unsplash

Welcome to another episode of the .NET Performance Series. This series features researches, benchmarks and comparisms of many different topics in the .NET world. The focus lies — as the title announces — on performance using the latest .NET 7. You are going to see which method is the fastest to implement specific topics as well as plenty of tips and tricks, how you can maximize your code performance with low effort. If you are interested in these topics, stay tuned!

In this article, we are going to compare the two most prominent json frameworks for .NET: Newtonsofts Json.NET and Microsofts System.Text.Json.

With over 2.3 billion downloads, Newtonsoft.Json is the most downloaded package on NuGet. System.Text.Json is a little behind with around 600 million downloads. However, we need to consider that System.Text.Json is delivered by default with the .NET SDK since .NET Core 3.1. That being said, Newtonsoft still seems to be the most favoured json framework. Let us find out, if it can keep up to its name or if Microsoft is slowly but surely getting ahead in terms of performance.

Benchmark Scenario

To simulate realistic scenarios of real life applications, we are going to test two main use cases:

  • First, the serialization and deserialization of single big datasets,
  • and second the serialization and deserialization of many small datasets.

A realistic scenario also needs realistic data. For a test data set, I decided to use the NuGet package Bogus. With Bogus, I am able to quickly generate many different users, with individual names, emails, ids etc.

For the benchmarks, we are going to use the lastest versions of each package, which are currently (October 2022):

  • Newtonsoft.Json — 13.0.1 and
  • System.Text.Json — 7.0.0-rc.2

Serialization Benchmarks

Serialize Big Data Object

To test the serialization of one big object, we simply use the List<User>, which we set up during our GlobalSetup() method. Our benchmark methods look like this:

These methods both use the default ContractResolver, which is only instantiated once and thus the best performing option of serializing for both frameworks. If you use custom JsonSerializerSettings, pay attention to not instantiate the ContractResolver more than once, or else you will loose performance.

Now let’s have a look at the results:

Even though Newtonsoft states on their first documentation site:

High performance: faster than .NET’s built-in JSON serializers

we can clearly see, they are by far not faster than the built-in JSON serializers. At least for this use case. Let’s see, if it might be true for other use cases.

Serialize Many Small Data Objects

This use case is more common in real life applications, e.g. in REST-Apis, where every web request has to handle JSON serialized data and also responds with JSON serialized data.

To implement this use case, we use our previously built List<User> and simply loop through it while serializing every single user individually:

On my machine, this benchmark leads to the following result:

We can see a near 100% faster performance again for many small objects. Not only the performance for System.Text.Json is twice as fast than Newtonsoft, but the heap allocated memory is even 5 times less! As mentioned in my previous articles, saving heap memory is even more important than the speed, you are seeing here. Heap memory will eventually have to be garbage collected, which will block your entire application from executing.

Deserialization Benchmarks

In real world applications, you do not only have to serialize, but also deserialize objects from a JSON serialized string. In the following benchmarks, we are going to use Bogus again, to create a set of users, but this time we ar going to serialize them into one big string for the big data object and a List<string> for the many small data objects.

Deserialize Big Data Object

The first deserialization benchmark deserialzes one big JSON string into the respective .NET object. In this case it is the List<User> again, which we also used in the previous examples.

Running these benchmarks on my machine, leads to the following results:

In terms of performance, Microsoft is still way ahead of Newtonsoft. However, we can see, that Newtonsoft is not half as slow, but around 40% slower, which is a gain in direct comparism with the serialization benchmark.

Deserialize Many Small Data Objects

The last benchmark in this episode is the deserialization of many small objects. Here we use the List<string> which we initialized in the GlobalSetup() method above, to deserialize the data objects in a loop:

The results are even more astonishing, than in the associated serializing benchmark:

The speed is again twice as fast and the memory efficiency is even 7 times better in Microsofts framework, than Newtonsoft!

Conclusion

Even though Newtonsoft states on their documentation:

High performance: faster than .NET’s built-in JSON serializers

It is obvious that— at least since .NET 7 — Microsofts System.Text.Json is at least twice as fast in all tested use cases, namingly:

  • Serializing one big data set
  • Serializing many small data sets
  • Deserializing one big data set
  • Deserializing many small data sets

All with the default serializer settings of each framework.

Not only the speed is 100% faster, but also the allocated memory is in some scenarios even more than 5 times more efficient than with Newtonsoft.

I even think, it is possible to extrapolate the results and state, that it is currently always faster to use System.Text.Json instead of Newtonsoft.Json.

Keep in mind, that these results are only valid for the latest .NET 7. If you are using other versions of .NET, it might be the other way round and Newtonsoft could be faster.

Also have a look at the other posts in this series:

The .NET Performance Series is backed by a Github Repository of mine, which can be found here:

I hope, I could facilitate your choice of serializers and give you an interesting insight in the world of performance and benchmarking.

Thank you for taking the time to read this article. I hope, you found it informative, educational and enjoyable. Your support and engagement are greatly appreciated.

If you’re interested in staying up-to-date with the latest trends, tips, and tricks for clean architecture, clean coding, and the latest tech stacks — especially in the context of C#, .NET and Angular — I would appreciate it if you considered following me.

Have a wonderful day!

If you are not yet using Medium to grow your knowledge everyday, now is the perfect time to get started! With Medium, you can easily gain more knowledge on highly professional topics, publish high-quality content and reach a wider audience. To get started, simply create a Medium account using this link:

Join Medium now

By doing so, you’ll gain access to a powerful platform that can help you connect with new writers and readers, and learn new things everyday.

Software Development
Software Engineering
Technology
Csharp
Dotnet
Recommended from ReadMedium