当前位置:网站首页>2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)

2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)

2022-07-04 19:46:00 With code written on his hand

2022 Years. , Made a new one json path Compatibility and performance test . Comparative testing of three popular frameworks in the market .

disclaimer : Maybe the wrong way to test results in unscientific results ( in addition , Different machines will have different results ), You can point it out in a message . The following test values are only valid for my computer ( To configure :Macbook pro 13 2020 paragraph i7+32G+1T).

The three frameworks and versions used in the test of this case :
  • com.alibaba.fastjson2:fastjson2:2.0.4
  • com.jayway.jsonpath:json-path:2.2.0
  • org.noear:snack3:3.2.31
Three samples of test data :
  • A:{code:1,msg:'Hello world',data:{list:[1,2,3,4,5], ary2:[{a:2},{a:3,b:{c:'ddd'}}]}}
  • B:[{b:{c:1}}, {b:{d:1}}, {b:{c:2}}, {b:{c:23}}]
  • C:[{c:'aaaa'}, {b:'cccc'}, {c:'cccaa'}]

Test plan :

1. Test pseudo code

var text = "...";

//1. Pre parse json
var obj = JSON.parse(text);

long start = System.currentTimeMillis();
for(int i=0,len=1000000; i<len; i++) {
    //2. perform json path
    JSONPath.eval(obj,"$..."); 
}
//3.100 Ten thousand times of consuming time ( That's the number recorded )
long times = System.currentTimeMillis() - start;

System.out.println(times);

2. Every frame 、 Each expression executes 4 Time , And record it 3 Time consuming

3. Finally, make a comparison table

4. test result :

Json path expression data fastjson2json-pathsnack3
$..aA872,764,7152658,2633,2590225,225,232
$..*A( Are not compatible 1)3227,3220,3156306,315,325
$.data.list[1,4]A577,524,419782,798,776133,137,131
$.data.list[1:4]A332,367,391941,899,947143,145,146
$.data.ary2[1].aA315,339,329704,663,65584,86,80
$.data.ary2[*].b.cA642,645,6601105,1025,1050239,235,237
$..b[?(@.c == 12)]B( Are not compatible 2)5628,5739,5636580,535,532
$..c.min()B( Are not compatible 2)( Are not compatible 2)279,282,285
$[?(@.c =~ /a+/)]C( Are not compatible 2)3575,3591,3813444,423,429
$..ary2[0].aA735,728,7362522,2551,2591310,311,314
$.data.list[?(@ in $..ary2[0].a)]A( Are not compatible 2)5494,5326,5483678,674,667

notes :

  • Are not compatible 1 : Returns an empty array directly
  • Are not compatible 2 : Direct anomaly

summary

  • fastjson2 Poor compatibility
  • json-path Poor performance , Functions are limited
  • snack3 Best performance , Support two strategies :1. The standard model , Keep up with json-path Compatible effects ;2. Non standard mode , Functions have more room to use

attach 1:snack3 Project address :

attach 2:.. And function expression compatibility

json-path:(snack3 The standard model is the same as ), The strategy is as follows :
  1. The order in which the selectors are executed :( The expressions in parentheses are processed and then aggregated )
    • $..(ary2[0].a)
    • $..(c.min()) // If c Is not an array , There will be an error here
    • $..(ary2[0][0])
  2. Can only :data.list[?(@ in $..ary2[0].a)]
  3. Can only : Execute the function on the original array node
snack3:(snack3 The nonstandard model of ), The strategy is as follows :
  1. The order in which the selectors are executed :( The expressions in parentheses are processed and then aggregated )

    • ($..ary2[0]).a
    • ($..c).min() //c Is the array normal // In practice , This will be more convenient , Also compatible with other expressions
    • ($..ary2[0])[0] // This will lead to different results // But in practice, there are very few
  2. Sure :data.list[?(@ in $..ary2[0].a)] or data.list[?(@ == $..ary2[0].a[0])]

  3. Sure : Execute the function on the original array node or Execute on query results

attach 3: Test code

原网站

版权声明
本文为[With code written on his hand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041815012367.html