Skip to content

Instantly share code, notes, and snippets.

@bigpigeon
Created February 28, 2023 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigpigeon/93f90fe84cf96f10356541c22db6e7dc to your computer and use it in GitHub Desktop.
Save bigpigeon/93f90fe84cf96f10356541c22db6e7dc to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"io"
"strings"
"testing"
)
const benchmarhData = `{
"a": "1",
"b": {"c": {"d": {"e": {"f": "2"}},
"d2": 22,
"d3": [{"e3": {"f4":"4"}}, {"e4": 44}]
}
},
"g": "3"
}`
func BenchmarkJsonDecode(b *testing.B) {
for i := 0; i < b.N; i++ {
var v interface{}
err := json.NewDecoder(strings.NewReader(benchmarhData)).Decode(&v)
if err != nil {
b.Fatal(err)
}
}
b.ReportAllocs()
}
func getAllToken(decoder *json.Decoder) {
for {
_, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
}
}
func BenchmarkGetToken(b *testing.B) {
for i := 0; i < b.N; i++ {
getAllToken(json.NewDecoder(strings.NewReader(benchmarhData)))
}
b.ReportAllocs()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment