How to Optimize JSON Encoding/Decoding in Go

Speed up Go JSON processing by reusing buffers and pre-allocating memory to reduce runtime allocations.

Optimize JSON encoding and decoding in Go by reusing buffers and pre-allocating slices to minimize memory allocations. Use json.Decoder with a bytes.Buffer for streaming large payloads, or pre-allocate []byte slices for known sizes before calling json.Marshal or json.Unmarshal.

import (
	"bytes"
	"encoding/json"
)

// Pre-allocate buffer for known sizes to reduce allocations
var buf bytes.Buffer

// Reuse buffer for multiple marshals
buf.Reset()
err := json.Compact(&buf, data)

// Use Decoder for streaming large JSON
decoder := json.NewDecoder(reader)
for decoder.More() {
	var item MyStruct
	decoder.Decode(&item)
}