Go: Is the encoding/json Package Really Slow?

Vincent
A Journey With Go
Published in
6 min readMay 20, 2019

--

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ This article is based on Go 1.12.

Questions about the performance of the encoding/json package is a recurrent topic and multiple libraries like easyjson, jsoniter or ffjson are trying to address this issue. But is it really slow? Has it been improved?

Evolution of the package

Let’s look first at the performance evolution of the library. I made a small makefile with a benchmark file in order to run it against all versions of go:

type JSON struct {
Foo int
Bar string
Baz float64
}

func BenchmarkJsonMarshall(b *testing.B) {
j := JSON{
Foo: 123,
Bar: `benchmark`,
Baz: 123.456,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(&j)
}
}

func BenchmarkJsonUnmarshal(b *testing.B) {
bytes := `{"foo": 1, "bar": "my string", bar: 1.123}`
str := []byte(bytes)
b.ResetTimer()
for i := 0; i < b.N; i++ {
j := JSON{}
_ = json.Unmarshal(str, &j)
}
}

The makefile creates a folder for each version of go, creates a container based on its docker image, and runs the benchmark. The results are compared in two ways:

  • each version VS the last version of go 1.12
  • each version VS the next version

--

--