← Back to list

Stop Using Go’s encoding/json Blindly, Meet Gamechanger Sonic

JSON (JavaScript Object Notation) is just a way to represent data as text.

Harsh Vishnoi · 2026-05-02 03:14 · 0 claps · 2.8 min read
#encoding #json #sonic #optimisation #golang
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development

Stop Using Go’s encoding/json Blindly, Meet Gamechanger Sonic

JSON (JavaScript Object Notation) is just a way to represent data as text.

Example:

{
  "name": "Harsh",
  "age": 25
}

In Go, you convert between:

  • JSON → Struct (Unmarshal)
  • Struct → JSON (Marshal)

If you’ve worked with Go APIs, you’ve definitely used:

  • encoding/json (the default)
  • And maybe heard about sonic (the fast one everyone’s talking about)

🤔 Quick Thought

You probably wrote this sometime:

json.Unmarshal(data, &user)

Looks harmless, right?

Now imagine this line running:

  • 10 times/sec → fine
  • 1,000 times/sec → noticeable
  • 50,000 times/sec → 🔥 your bottleneck

Every request involves JSON parsing.

👉 That’s where performance becomes critical.

🧠 Analogy Time

Imagine this: You’re given a box (JSON), and you need to organize it into labeled shelves (struct).

encoding/json does this:

  1. Opens the box
  2. Look at each item
  3. Thinks: “Hmm… what is this? string? int? field name?”
  4. Decides where to put it

This “figuring out” step happens every single time, called reflection in technical terms.

The Problem: Reflection

encoding/json does the same, it figures out your struct at runtime

That means:

  • More CPU work
  • More memory usage
  • Slower execution

🤔 Pause & Think

If you had to sort the same type of box 1 million times…

Would you:

  • Re-learn everything every time ❌
  • Or memorize the structure once ✅

Meet bytedance/sonic

Sonic asks a simple question:

👉 “Why figure things out every time… when we can prepare in advance?”

But here’s the real question:

  • Why is Sonic actually faster?
  • And more importantly, when should you care?

Let’s break it down in the simplest way possible.

1. 🧠 It “Learns Once” (JIT Compilation)

Instead of figuring things out repeatedly, Sonic:

  • Study your struct once
  • Creates optimized machine code
  • Reuses it forever

👉 Think of it like:

Writing a shortcut instead of solving the problem again and again

2. 🧹 Less Garbage = Less Stress

encoding/json:

  • Creates lots of temporary objects
  • Go’s Garbage Collector has to clean them

Sonic:

  • Avoids creating unnecessary stuff
  • Reuses memory smartly

3. ⚡ Works on Multiple Data at Once (SIMD)

This is a fancy one, but here’s the simple version:

encoding/json:

  • Reads JSON character by character

Sonic:

  • Reads multiple characters at once

What does “reading multiple characters at once” mean?

👉 encoding/json

It processes JSON like this (simplified):

{ "name": "harsh" }
   ↑
read one byte → decide → move → repeat

It loops through one byte at a time, checking:

  • Is this { ?
  • Is this a quote " ?
  • Is this part of a string?

So internally it’s basically:

for i := 0; i < len(data); i++ {
    process(data[i])
}

⚡ Sonic (SIMD approach)

Instead of reading one character at a time, Sonic uses CPU vector instructions (SIMD).

Think of it like:

{ "name": "harsh" }
^^^^^^^^^^^^^^^^^^
read 16–64 bytes in one go

It loads a chunk of JSON data into CPU vector registers and processes multiple bytes in parallel. Instead of checking one character at a time, it applies vectorized operations (like XOR and comparisons) across 16–64 bytes at once, quickly identifying structural characters such as commas, braces, and quotes in a single pass.

4. 🚀 Pretouch (Warm-up Mode)

Sonic can prepare everything before real traffic hits.

So instead of:

  • First request beingslow

You get:

  • Consistent performance from the start

5: So… How Much Faster?

In real-world benchmarks:

👉 Sonic is often 2x to 5x faster

And sometimes even more for:

  • Large payloads
  • High-throughput systems

Use encoding/json when:

  • You want maximum safety
  • You need standard behavior
  • Performance is not critical

Use sonic when:

  • You handle large JSON payloads
  • You care about latency
  • You’re building high-performance APIs

At the end of the day, both encoding/json and bytedance/sonic solve the same problem — but the way they get there couldn’t be more different. One favors simplicity and reliability; the other pushes your hardware to its limits to unlock serious performance gains.

And when you’re working with large payloads or high-throughput systems, that gap stops being a detail — it becomes a competitive edge.

This is just the beginning. See you in the next post in the system optimization series 🚀


메타데이터
post_id
0a6d6b2a9a5d
slug
stop-using-gos-encoding-json-blindly-meet-gamechange-sonic-0a6d6b2a9a5d
url
https://medium.com/@harsh.vishnoi01/stop-using-gos-encoding-json-blindly-meet-gamechange-sonic-0a6d6b2a9a5d
canonical_url
https://medium.com/@harsh.vishnoi01/stop-using-gos-encoding-json-blindly-meet-gamechange-sonic-0a6d6b2a9a5d
author_url
https://medium.com/@harsh.vishnoi01
status
ok
fetched_at
2026-07-10 21:44:25