|
1 | 1 | [](https://goreportcard.com/report/github.com/buger/jsonparser) 
|
2 | 2 | # Alternative JSON parser for Go (so far fastest)
|
3 | 3 |
|
4 |
| -It does not require you to know the structure of the payload (eg. create structs), and allows accessing fields by providing the path to them. It is up to **10 times faster** then standard `encoding/json` package (depending on payload size and usage), **allocates no memory**. See benchmarks below. |
| 4 | +It does not require you to know the structure of the payload (eg. create structs), and allows accessing fields by providing the path to them. It is up to **10 times faster** than standard `encoding/json` package (depending on payload size and usage), **allocates no memory**. See benchmarks below. |
5 | 5 |
|
6 | 6 | ## Rationale
|
7 | 7 | Originally I made this for a project that relies on a lot of 3rd party APIs that can be unpredictable and complex.
|
@@ -149,7 +149,7 @@ jsonparser.ObjectEach(myJson, handler)
|
149 | 149 | ```go
|
150 | 150 | func KeyEach(data []byte, cb func(idx int, value []byte, dataType jsonparser.ValueType, err error), paths ...[]string)
|
151 | 151 | ```
|
152 |
| -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! |
| 152 | +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 than `Get`. Path can use nested keys as well! |
153 | 153 |
|
154 | 154 | ```go
|
155 | 155 | paths := [][]string{
|
@@ -203,7 +203,7 @@ Compared libraries:
|
203 | 203 |
|
204 | 204 | #### TLDR
|
205 | 205 | If you want to skip next sections we have 2 winner: `jsonparser` and `easyjson`.
|
206 |
| -`jsonparser` is up to 10 times faster then standard `encoding/json` package (depending on payload size and usage), and almost infinitely (literally) better in memory consumption because it operates with data on byte level, and provide direct slice pointers. |
| 206 | +`jsonparser` is up to 10 times faster than standard `encoding/json` package (depending on payload size and usage), and almost infinitely (literally) better in memory consumption because it operates with data on byte level, and provide direct slice pointers. |
207 | 207 | `easyjson` wins in CPU in medium tests and frankly i'm impressed with this package: it is remarkable results considering that it is almost drop-in replacement for `encoding/json` (require some code generation).
|
208 | 208 |
|
209 | 209 | It's hard to fully compare `jsonparser` and `easyjson` (or `ffson`), they a true parsers and fully process record, unlike `jsonparser` which parse only keys you specified.
|
@@ -235,7 +235,7 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_small_payloa
|
235 | 235 | | buger/jsonparser | **1367** | **0** | **0** |
|
236 | 236 | | buger/jsonparser (EachKey API) | **809** | **0** | **0** |
|
237 | 237 |
|
238 |
| -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. |
| 238 | +Winners are ffjson, easyjson and jsonparser, where jsonparser is up to 9.8x faster than encoding/json and 4.6x faster than ffjson, and slightly faster than easyjson. |
239 | 239 | 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.
|
240 | 240 |
|
241 | 241 | #### Medium payload
|
@@ -281,7 +281,7 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_large_payloa
|
281 | 281 | | mailru/easyjson | **154186** | **6992** | **288** |
|
282 | 282 | | buger/jsonparser | **85308** | **0** | **0** |
|
283 | 283 |
|
284 |
| -`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) |
| 284 | +`jsonparser` now is a winner, but do not forget that it is way more lighweight parser than `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) |
285 | 285 |
|
286 | 286 | 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.
|
287 | 287 |
|
|
0 commit comments