Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd34e5d4

Browse files
committed
wsjson: Add json.Encoder vs json.Marshal benchmark
json.Encoder is 42% faster than json.Marshal thanks to the memory reuse.goos: linuxgoarch: amd64pkg: nhooyr.io/websocket/wsjsoncpu: 12th Gen Intel(R) Core(TM) i5-1235UBenchmarkJSON/json.Encoder-12 3517579 340.2 ns/op 24 B/op 1 allocs/opBenchmarkJSON/json.Marshal-12 2374086 484.3 ns/op 728 B/op 2 allocs/opCloses#409
1 parentecf7dec commitd34e5d4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎wsjson/wsjson_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package wsjson_test
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"strings"
7+
"testing"
8+
)
9+
10+
funcBenchmarkJSON(b*testing.B) {
11+
msg:= []byte(strings.Repeat("1234",128))
12+
b.SetBytes(int64(len(msg)))
13+
b.ReportAllocs()
14+
b.Run("json.Encoder",func(b*testing.B) {
15+
fori:=0;i<b.N;i++ {
16+
json.NewEncoder(io.Discard).Encode(msg)
17+
}
18+
})
19+
b.Run("json.Marshal",func(b*testing.B) {
20+
fori:=0;i<b.N;i++ {
21+
json.Marshal(msg)
22+
}
23+
})
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp