0
0
Unityframework~8 mins

JSON serialization in Unity - Performance & Optimization

Choose your learning style9 modes available
Performance: JSON serialization
MEDIUM IMPACT
This affects how quickly data is converted to and from JSON format, impacting load times and responsiveness when saving or loading game data.
Serializing game data frequently during gameplay
Unity
string json = JsonUtility.ToJson(largeComplexObject, false);
Disabling pretty print reduces CPU work and output size, speeding up serialization and reducing memory pressure.
📈 Performance Gainreduces serialization time by 30-50%, lowers memory allocations
Serializing game data frequently during gameplay
Unity
string json = JsonUtility.ToJson(largeComplexObject, true);
Using pretty print (true) adds extra processing and larger output size, slowing serialization and increasing memory use.
📉 Performance Costblocks main thread for 10-50ms depending on object size, increases memory allocation
Performance Comparison
PatternCPU UsageMemory AllocationsFrame ImpactVerdict
Pretty print enabledHighHighCauses frame drops[X] Bad
Pretty print disabledMediumMediumSmooth frame rate[OK] Good
Repeated deserialization every frameVery HighHighSevere frame jank[X] Bad
Cache deserialized dataLowLowStable frames[OK] Good
Rendering Pipeline
JSON serialization runs on the CPU before rendering. Heavy serialization blocks the main thread, delaying frame updates and user input processing.
JavaScript/Mono runtime execution
Main thread blocking
⚠️ BottleneckCPU serialization/deserialization time blocking main thread
Optimization Tips
1Disable pretty print to speed up JSON serialization.
2Avoid serializing or deserializing JSON every frame.
3Cache deserialized objects to reduce CPU usage.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a common performance issue when using JSON serialization with pretty print enabled in Unity?
AIt increases CPU time and memory usage during serialization.
BIt reduces the JSON file size significantly.
CIt speeds up deserialization automatically.
DIt prevents garbage collection.
DevTools: Unity Profiler
How to check: Open Unity Profiler, record gameplay, look at CPU Usage and Garbage Collector sections during serialization calls.
What to look for: High spikes in CPU time and frequent GC allocations indicate inefficient JSON serialization.