You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you need to read multiple keys, and you do not afraid of low-level API `KeyEach` is your friend. It read payload only single time, and calls callback function once path is found. For example when you call multiple times `Get`, it has to process payload multiple times, each time you call it. Depending on payload `KeyEach` can be multiple times faster then `Get`. Path can use nested keys as well!
133
+
134
+
```
135
+
paths := [][]string{
136
+
[]string{"uuid"},
137
+
[]string{"tz"},
138
+
[]string{"ua"},
139
+
[]string{"st"},
140
+
}
141
+
var data SmallPayload
142
+
143
+
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error){
144
+
switch idx {
145
+
case 0:
146
+
data.Uuid, _ = value
147
+
case 1:
148
+
v, _ := jsonparser.ParseInt(value)
149
+
data.Tz = int(v)
150
+
case 2:
151
+
data.Ua, _ = value
152
+
case 3:
153
+
v, _ := jsonparser.ParseInt(value)
154
+
data.St = int(v)
155
+
}
156
+
}, paths...)
157
+
```
158
+
128
159
129
160
## What makes it so fast?
130
161
* It does not rely on `encoding/json`, `reflection` or `interface{}`, the only real package dependency is `bytes`.
Winners are ffjson, easyjson and jsonparser, where jsonparser is 5.5x faster then encoding/json and 2.8x faster then ffjson, and slightly faster then easyjson.
218
+
Winners are ffjson, easyjson and jsonparser, where jsonparser is up to 9.8x faster then encoding/json and 4.6x faster then ffjson, and slightly faster then easyjson.
187
219
If you look at memory allocation, jsonparser has no rivals, as it makes no data copy and operates with raw []byte structures and pointers to it.
The difference between ffjson and jsonparser in CPU usage is smaller, while the memory consumption difference is growing. On the other hand `easyjson` shows remarkable performance for medium payload.
`jsonparser` now is a winner, but do not forget that it is way more lighweight parser then `ffson` or `easyjson`, and they have to parser all the data, while `jsonparser` parse only what you need. All `ffjson`, `easysjon` and `jsonparser` have their own parsing code, and does not depend on `encoding/json` or `interface{}`, thats one of the reasons why they are so fast. `easyjson` also use a bit of `unsafe` package to reduce memory consuption (in theory it can lead to some unexpected GC issue, but i did not tested enough)
232
265
266
+
Also last benchmark did not included `EachKey` test, because in this particular case we need to read lot of Array values, and using `ArrayEach` is more efficient.
267
+
233
268
## Questions and support
234
269
235
270
All bug-reports and suggestions should go though Github Issues.
0 commit comments