good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit d34e5d4b authored by Anmol Sethi's avatar Anmol Sethi
Browse files

wsjson: Add json.Encoder vs json.Marshal benchmark

json.Encoder is 42% faster than json.Marshal thanks to the memory reuse.

goos: linux
goarch: amd64
pkg: nhooyr.io/websocket/wsjson
cpu: 12th Gen Intel(R) Core(TM) i5-1235U
BenchmarkJSON/json.Encoder-12            3517579           340.2 ns/op        24 B/op          1 allocs/op
BenchmarkJSON/json.Marshal-12            2374086           484.3 ns/op       728 B/op          2 allocs/op

Closes #409
parent ecf7dec4
Branches
Tags
Loading
package wsjson_test
import (
"encoding/json"
"io"
"strings"
"testing"
)
func BenchmarkJSON(b *testing.B) {
msg := []byte(strings.Repeat("1234", 128))
b.SetBytes(int64(len(msg)))
b.ReportAllocs()
b.Run("json.Encoder", func(b *testing.B) {
for i := 0; i < b.N; i++ {
json.NewEncoder(io.Discard).Encode(msg)
}
})
b.Run("json.Marshal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Marshal(msg)
}
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment