Skip to content

Commit 148fd82

Browse files
authored
Add EachKey docs
1 parent 5048f35 commit 148fd82

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,37 @@ func ArrayEach(data []byte, cb func(value []byte, dataType jsonparser.ValueType,
125125
```
126126
Needed for iterating arrays, accepts a callback function with the same return arguments as `Get`.
127127

128+
### **`KeyEach`**
129+
```
130+
func KeyEach(data []byte, cb func(idx int, value []byte, dataType jsonparser.ValueType, err error), paths ...[]string)
131+
```
132+
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+
128159

129160
## What makes it so fast?
130161
* It does not rely on `encoding/json`, `reflection` or `interface{}`, the only real package dependency is `bytes`.
@@ -182,8 +213,9 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_small_payloa
182213
| pquerna/ffjson | **3769** | **624** | **15** |
183214
| mailru/easyjson | **2002** | **192** | **9** |
184215
| buger/jsonparser | **1367** | **0** | **0** |
216+
| buger/jsonparser (EachKey API) | **809** | **0** | **0** |
185217

186-
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.
187219
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.
188220

189221
#### Medium payload
@@ -205,6 +237,7 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_medium_paylo
205237
| pquerna/ffjson | **20298** | **856** | **20** |
206238
| mailru/easyjson | **10512** | **336** | **12** |
207239
| buger/jsonparser | **15955** | **0** | **0** |
240+
| buger/jsonparser (EachKey API) | **8916** | **0** | **0** |
208241

209242
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.
210243

@@ -230,6 +263,8 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_large_payloa
230263

231264
`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)
232265

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+
233268
## Questions and support
234269

235270
All bug-reports and suggestions should go though Github Issues.

0 commit comments

Comments
 (0)