Open
Description
type Demo struct {
A int `json:"a,omitempty"`
B string `json:"b,omitempty"`
}
func BenchmarkJsoncost(b *testing.B) {
d := Demo{
A: 1,
B: "abc",
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := json.Marshal(d)
if err != nil {
b.Errorf("marshal failed: %v", err)
}
}
}
go test -run ^NOTHING -bench Jsoncost$ -v
goos: darwin
goarch: amd64
pkg: go-app/basic
BenchmarkJsoncost-8 5000000 318 ns/op 64 B/op 2 allocs/op
PASS
Then, I use ffjson to generate custom MarshalJSON for Demo struct, update json.Marshal(d)
to ffjson.Marshal(d)
, run benchmark again
goos: darwin
goarch: amd64
pkg: go-app/basic
BenchmarkJsoncost-8 5000000 355 ns/op 64 B/op 2 allocs/op
PASS
It seems ffjson is slower than encoding/json, is there anything I'm missing ?
Metadata
Metadata
Assignees
Labels
No labels