DEV Community

Akshit Zatakia
Akshit Zatakia

Posted on

EasyJSON: Supercharge JSON Performance in Go

Introduction: Why EasyJSON Stands Out

JSON serialization and deserialization are at the heart of many Go applications, from web servers to microservices. While Go’s encoding/json package is reliable, it often struggles with performance when processing large data structures.

That’s where EasyJSON comes into play. Designed for speed and low memory consumption, EasyJSON generates serialization code at compile time, eliminating runtime reflection overhead. In this blog, we’ll explore how EasyJSON works, compare it to other libraries, and demonstrate why it’s a game-changer for performance-hungry applications.


Why Choose EasyJSON for JSON Serialization?

  1. Performance: Outperforms Go’s standard library and most third-party solutions.
  2. Zero Runtime Reflection: Generates static code, avoiding costly reflection.
  3. Lightweight: Reduces memory allocations significantly.
  4. Compatibility: Works seamlessly with existing Go structs.

Getting Started with EasyJSON

Installation

go get github.com/mailru/easyjson && go install github.com/mailru/easyjson/...@latest
Enter fullscreen mode Exit fullscreen mode

Ensure gopath is in your PATH for generating code

export GOPATH=/Users/<username>/go
export PATH=$GOPATH/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

Generating Code for Your Structs

  1. Define your Go struct:
package main  

type User struct {  
    ID   int    `json:"id"`  
    Name string `json:"name"`  
    Age  int    `json:"age"`  
}
Enter fullscreen mode Exit fullscreen mode
  1. Generate EasyJSON code:
easyjson -all user.go
Enter fullscreen mode Exit fullscreen mode

This creates user_easyjson.go, containing optimized serialization methods for your struct.


Code Example: Using EasyJSON in Your Application

package main  

import (  
    "fmt"  
    "github.com/mailru/easyjson"  
)  

//easyjson:json  
type User struct {  
    ID   int    `json:"id"`  
    Name string `json:"name"`  
    Age  int    `json:"age"`  
}  

func main() {  
    user := User{ID: 1, Name: "John Doe", Age: 30}  

    // Serialize
    data, err := easyjson.Marshal(user)  
    if err != nil {  
        panic(err)  
    }  
    fmt.Println("Serialized JSON:", string(data))  

    // Deserialize  
    var deserialized User  
    if err := easyjson.Unmarshal(data, &deserialized); err != nil {  
        panic(err)  
    }  
    fmt.Println("Deserialized Struct:", deserialized)  
}

Enter fullscreen mode Exit fullscreen mode

Benchmarking EasyJSON vs. Alternatives

Setup
We compared EasyJSON with encoding/json and json-iterator using a struct with nested fields and lists.

Here is the Github repo which has the code for benchmarking.

Results

| Library         | Serialization (ns/op) | Deserialization (ns/op) | Allocations (B/op) | Allocations (#) |
|-----------------|-----------------------|--------------------------|--------------------|------------------|
| `encoding/json` | 13,000               | 14,000                  | 1,500              | 15               |
| `json-iterator` | 8,500                | 9,200                   | 800                | 10               |
| **EasyJSON**    | **4,500**            | **4,800**               | **100**            | **1**            |
Enter fullscreen mode Exit fullscreen mode

Takeaway: EasyJSON is up to 3x faster than encoding/json and minimizes memory allocations dramatically.


Comparison: EasyJSON vs. Other JSON Libraries

| Feature                   | `encoding/json` | `json-iterator` | **EasyJSON**   |
|---------------------------|-----------------|-----------------|----------------|
| Reflection-Free           | ✗               | ✗               | ✅             |
| Performance               | Medium          | High            | **Very High**  |
| Memory Efficiency         | Medium          | High            | **Very High**  |
| Code Generation Required? | ✗               | ✗               | ✅             |
| API Simplicity            | ✅               | ✅               | ✅             |

Enter fullscreen mode Exit fullscreen mode

When Should You Use EasyJSON?

EasyJSON is ideal for:

  1. High-throughput APIs requiring low-latency serialization.
  2. Memory-constrained environments where allocations must be minimized.
  3. Applications where compile-time code generation is acceptable.

Caveats of Using EasyJSON

  • Code Generation: Requires an extra step in your build process.
  • Maintenance: Any changes to structs necessitate regenerating EasyJSON code.

Conclusion

EasyJSON is a powerful tool for developers seeking unmatched performance and efficiency in JSON processing. While it introduces some overhead in terms of code generation, the benefits far outweigh the drawbacks in performance-critical applications.

If speed and memory efficiency matter to your Go projects, it’s time to give EasyJSON a spin! 🚀


Do you use EasyJSON or another library? Share your experiences in the comments below!

Top comments (1)

Collapse
 
dzungnt98 profile image
Dzung Nguyen

Thanks for sharing!