JsonPath greater compatibility and performance testing
Recently gave their json
framework snack3
adds json path
support. After a good job, to find the two popular market framework for comparative testing to help improve their own performance and compatibility framework.
After measuring circle, we found snack3
good compatibility, performance is the best. In the evening to drink a little wine to celebrate! ! !
Disclaimer: I am a java novice, may have to test the wrong way caused by unscientific results, you can shout it out. The following test values valid only on my computer (configuration: Macbook pro 13 2018 models i7 + 16G + 512G).
Snack3 signed by Javascript
all of the variables var
stated, and Xml dom
everything is Node
design. Under which all data are to ONode
represent, ONode
namely One node
Italy, on behalf of any type can be converted to any type.
- Manipulation of the document tree and emphasize building skills
- As a media center, easy conversion between different formats
- High-performance
Json path
query (compatibility and performance is praise) - stand by
序列化、反序列化
Test case and three frame versions with:
- com.alibaba:fastjson:1.2.29
- com.jayway.jsonpath:json-path:2.2.0
- org.noear:snack3:3.1.5.9
Three test data sample:
- 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 program:
1. Test Pseudocode
var text = "...";
//1.预解析json
var obj = JSON.parse(text);
long start = System.currentTimeMillis();
for(int i=0,len=1000000; i<len; i++) {
//2.执行json path
JSONPath.eval(obj,"$...");
}
//3.100万次的消耗时间(记录的数值就是这个)
long times = System.currentTimeMillis() - start;
System.out.println(times);
2. Each frame, each expression implementing four times, three times and after the recording time consuming
3. Finally made comparison table
4. Test results:
Json path expression | data | fastjson | json-path | snack3 |
---|---|---|---|---|
$..a |
A | 529,546,539 | 2658,2633,2590 | 225,225,232 |
$..* |
A | (Incompatible 1) | 3227,3220,3156 | 306,315,325 |
data.list[1,4] |
A | 85,90,86 | 782,798,776 | 133,137,131 |
data.list[1:4] |
A | 105,109,109 | 941,899,947 | 143,145,146 |
data.ary2[1].b.c |
A | 60,58,58 | 929,826,837 | 84,86,80 |
data.ary2[*].b.c |
A | (Incompatible 2) | 1105,1025,1050 | 173,152,155 |
$..b[?(@.c == 12)] |
B | (Incompatible 2) | 5628,5739,5636 | 580,535,532 |
$..c.min() |
B | (Incompatible 2) | (Incompatible 2) | 279,282,285 |
$[?(@.c =~ /a+/)] |
C | (Incompatible 2) | 3575,3591,3813 | 444,423,429 |
$..ary2[0].a |
A | 325,321,319 | 2522,2551,2591 | 310,311,314 |
data.list[?(@ in $..ary2[0].a)] |
A | (Incompatible 2) | 5494,5326,5483 | 678,674,667 |
Note:
- Not compatible with 1: direct return to the root own
- Not compatible with 2: Direct abnormal
to sum up
- There is no compatibility at all fastjson
- json-path performance is not satisfactory, the limitations of using a large function
- snack3 best performance, support two strategies: a standard mode, to maintain compatibility with json-path effects; nonstandard mode 2, the use of a larger room for the function.
Annex 1: snack3 Project Address:
- https://github.com/noear/snack3
- https://gitee.com/noear/snack3
Annex 2: .. Compatibility and described function expression of
json-path: (Standard Mode snack3 same), the processing policy is as follows:
- The execution order of the selector parentheses :( expression processing after polymerization)
$..(ary2[0].a)
$..(c.min())
// if c is not an array, here to be wrong$..(ary2[0][0])
- Only:
data.list[?(@ in $..ary2[0].a)]
- Only: to perform functions on the original array node
snack3: (snack3 non-standard mode), the processing policy is as follows:
- The execution order of the selector parentheses :( expression processing after polymerization)
($..ary2[0]).a
($..c).min()
// c is not an array normal // actual use, this would be more convenient, but also compatible with other expressions($..ary2[0])[0]
// This will cause different results, but in practice rarely occurs //
Can:
data.list[?(@ in $..ary2[0].a)]
ordata.list[?(@ == $..ary2[0].a[0])]
Can: performed on the original array node function or execute the query results
Annex 3: Test Code
com.alibaba:fastjson
Test code: https: //gitee.com/noear/snack3/blob/master/snack3_demo/src/test/java/speed/SpeedFastjsonJsonPathTest.javacom.jayway.jsonpath:json-path
Test code: https: //gitee.com/noear/snack3/blob/master/snack3_demo/src/test/java/speed/SpeedJaywayJsonPathTest.javaorg.noear:snack3
Test code: https: //gitee.com/noear/snack3/blob/master/snack3_demo/src/test/java/speed/SpeedJsonPathTest.java