Just Eat Israel Abramov Feb 25, 2021

JSON Serialization Libraries Performance Comparison

Article Summary

Israel Abramov from Just Eat Takeaway tested 10 JSON serialization libraries across 8 scenarios. The results? Your default serializer might be costing you serious performance.

Most .NET developers default to Newtonsoft.Json, but it has a fundamental inefficiency: it treats UTF-8 data as UTF-16, creating unnecessary overhead. This comprehensive benchmark tested execution time, memory allocation, and CPU usage across serialization and deserialization of both simple and complex objects.

Key Takeaways

Critical Insight

Switching from Newtonsoft.Json to SpanJson, Utf8Json, or System.Text.Json can dramatically improve API performance with minimal code changes.

The article reveals which popular library causes unhandled stack overflow exceptions and why Microsoft's own legacy serializer should never be used.

About This Article

Problem

Israel Abramov found that Newtonsoft.Json's UTF-8 to UTF-16 conversion adds overhead when REST APIs handle HTTP requests and responses. The team needed to validate performance across different serialization libraries.

Solution

They used BenchmarkDotNet to measure execution time and memory allocation across 10 libraries in 8 scenarios. System.Diagnostic's PerformanceCounter tracked CPU usage on both simple and complex object structures.

Impact

The testing showed a 10x difference in memory allocation between the best and worst performers on complex objects. SpanJson hit 321.8 nanoseconds execution time, while System.Runtime.Serialization.Json was significantly slower.