skip to content
Alvin Lucillo

Benchmark for binary encoding

/ 1 min read

Using yesterday’s benchmark test, the manual binary encoding is included, so we can see the difference between the two in terms of benchmark result

func BenchmarkEncodeJSON(b *testing.B) {
	for b.Loop() {
		if _, err := EncodeJSON(benchmarkEvent); err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkEncodeBinary(b *testing.B) {
	for b.Loop() {
		_ = EncodeBinary(benchmarkEvent)
	}
}

Below, the two benchmarks were run. We can see that binary encoding is 5 times faster (150.0/29.92) than JSON encoding. Since ns/op means the number of nanoseconds it took to complete an operation, it means binary encoding is efficient at encoding process.

go test -run '^$' -bench '^BenchmarkEncode'
goos: linux
goarch: arm64
pkg: codec01
BenchmarkEncodeJSON-12           8147055               150.0 ns/op
BenchmarkEncodeBinary-12        37581630                29.92 ns/op
PASS
ok      codec01 2.352s