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
- SpanJson dominated with best execution time and memory allocation across tests
- Newtonsoft.Json underperformed against newer alternatives like System.Text.Json
- Utf8Json and NetJSON showed remarkable speed but crash on circular references
- Memory allocation varied 10x between best and worst performers on complex objects
- System.Runtime.Serialization.Json ranked worst, avoid for production use
Switching from Newtonsoft.Json to SpanJson, Utf8Json, or System.Text.Json can dramatically improve API performance with minimal code changes.
About This Article
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.
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.
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.