As an important data format, Json has good readability and self-description, and is widely used in various data transmission scenarios. Go inside language native support for the serialization and deserialization of the data format, the internal use of reflection mechanism, performance is a little difference, in analytical applications is highly dependent on json, tend to become the performance bottleneck, but have a lot of third-party libraries to help us solve the problem, but so many libraries, to have such as I choose difficult disease, How to choose in the end, the following to everyone to one analysis

ffjson

go get -u github.com/pquerna/ffjsonCopy the code

The main reason for the poor performance of the native library is the use of many reflection mechanisms. To solve this problem, FFJSON generates code through pre-compilation, and the type determination is determined at the pre-compilation stage, avoiding reflection at run time

However, one more step is needed before compiling, which requires you to generate FFJSON code. The generation code only needs to execute ffjson

, where file.go is a GO file that contains the definition of JSON structure. $GOPATH/bin = $GOPATH/bin = $GOPATH/bin = $GOPATH/bin = $GOPATH/bin = $GOPATH/bin

In addition, if you have some structure and don’t want FFJSON to generate code, you can add comments

// ffjson: skip
type Foo struct {
   Bar string
}

// ffjson: nodecoder
type Foo struct {
   Bar string
}Copy the code

easyjson

go get -u github.com/mailru/easyjson/...Copy the code

Easyjson idea and FFJSON is consistent, is to increase a pre-compiled process, pre-generate the corresponding structure of the serialization deserialization code, in addition, easyJSON also abandoned some of the original library surface support some unnecessary features, such as: Key type declaration, key case insensitive and so on to achieve higher performance

Easyjson -all

if you do not specify -all, only //easyjson:json structures will generate code

//easyjson:json
type A struct {
    Bar string
}Copy the code

jsoniter

go get -u github.com/json-iterator/goCopy the code

This is a very magical library, didi developed, unlike easyJSON and FFJSON are used pre-compiled, and 100% compatible with native library, but super good performance, also do not know how to achieve, if anyone knows, can tell me?

To use the above, you just put all the

import "encoding/json"Copy the code

replace

import "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibraryCopy the code

That’s it. Nothing else needs to be touched

codec-json

go get -u github.com/ugorji/go/codecCopy the code

There are a lot of things in this library, and JSON is just one feature that is old, cumbersome to use, and not performing very well

jsonparser

go get -u github.com/buger/jsonparserCopy the code

Strictly speaking, this library does not belong to the json serialization library, but only provides some json parsing interface. When using this library, you need to set the values in the structure. In fact, every time you call it, you need to re-parse the JSON object, which is not very good performance

As the name implies, this library is only a parsing library, with no serialization interface

The performance test

The above json library, made some performance tests, test code at: github.com/hatlonely/…. Here are the results of my test on my Macbook (the actual results depend on the version of the library and the machine environment, I recommend you to test again) :

BenchmarkMarshalStdJson-4                    1000000          1097 ns/op
BenchmarkMarshalJsonIterator-4               2000000           781 ns/op
BenchmarkMarshalFfjson-4                     2000000           941 ns/op
BenchmarkMarshalEasyjson-4                   3000000           513 ns/op
BenchmarkMarshalCodecJson-4                  1000000          1074 ns/op
BenchmarkMarshalCodecJsonWithBufio-4         1000000          2161 ns/op
BenchmarkUnMarshalStdJson-4                   500000          2512 ns/op
BenchmarkUnMarshalJsonIterator-4             2000000           591 ns/op
BenchmarkUnMarshalFfjson-4                   1000000          1127 ns/op
BenchmarkUnMarshalEasyjson-4                 2000000           608 ns/op
BenchmarkUnMarshalCodecJson-4                  20000        122694 ns/op
BenchmarkUnMarshalCodecJsonWithBufio-4        500000          3417 ns/op
BenchmarkUnMarshalJsonparser-4               2000000           877 ns/opCopy the code

As can be seen from the results above:

  1. Easyjson is optimal for both serialization and deserialization, with serialization improved by a factor of 1 and deserialization improved by a factor of 3
  2. Jsoniter also performs very well, close to EasyJSON, but with no precompilation process and 100% compatibility with native libraries
  3. The serialization improvement of FFJSON is not obvious, and the deserialization is improved by a factor of 1
  4. Codecjson is not much worse than the native library, if not worse
  5. Jsonparser is not well suited for such scenarios, the performance gains are not significant, and there is no deserialization

Therefore, it is recommended to use Jsoniter, if the pursuit of ultimate performance, consider easyJSON

Refer to the link

Ffjson: github.com/pquerna/ff…. Easyjson: github.com/mailru/eas…. Jsoniter: github.com/json-itera…. Jsonparser: github.com/buger/json…. Codecjson: ugorji.net/blog/go-cod…

Please indicate the source of reprint


Links to this article:
hatlonely.github.io/20…