Skip to content

performance issue #256

Description

@jiacai2050
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 ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions