11# Performance
22
3- Javascript internally uses UTF-16/UCS-2 encoding for strings while libxml uses UTF-8 .
4- This means when we need to convert them on every function invocation .
5- Among these invocations, parsing is most impacted because the entire xml needs to be converted .
3+ One of the challenges of working with Javascript and libxml is that they use different encodings for strings .
4+ Javascript uses UTF-16/UCS-2, while libxml uses UTF-8 .
5+ This means that we have to convert the strings every time we call a function from libxml .
66
7- This library provides two set of parsing functions: String API and Buffer API.
8- Please use the ** Buffer API** whenever possible to get best throughput.
7+ To avoid this overhead, this library offers two types of parsing functions: String API and Buffer API.
8+ The String API takes a Javascript string as input and converts it to a UTF-8 buffer before parsing.
9+ The Buffer API takes a UTF-8 buffer as input and parses it directly.
10+ The Buffer API is faster than the String API, especially for large XML documents.
11+ Therefore, we recommend using the Buffer API whenever possible to improve the performance of your code.
912
1013## Benchmark
1114
15+ The benchmark report below is for the performance of DOM tree parsing, among the following libraries
16+
17+ - Bindings of libxml
18+ - String API of libxml2-wasm
19+ - Buffer API of libxml2-wasm
20+ - [ libxmljs2] ( https://www.npmjs.com/package/libxmljs2 )
21+ - Pure javascript implementation
22+ - [ @rgrove/parse-xml ] ( https://www.npmjs.com/package/@rgrove/parse-xml )
23+ - [ fast-xml-parser] ( https://www.npmjs.com/package/fast-xml-parser )
24+ - [ xmldoc] ( https://www.npmjs.com/package/xmldoc )
25+
1226To run the benchmark, build the lib first,
1327
1428``` sh
@@ -22,62 +36,70 @@ cd benchmark && npm ci
2236npm test
2337```
2438
25- ` libxmljs2 ` is js binding to native library libxml2; while ` fast-xml ` and ` xmldoc ` are pure javascript implementations.
2639
2740```
28- Environment: NodeJs v18.17.1 on Darwin x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
41+ Environment: NodeJs v18.19.0 on Darwin arm64 Apple M3 Max
2942
30- Running "fixtures/small.xml: 787 bytes" suite...
43+ Running "fixtures/small.xml: 780 bytes" suite...
3144Progress: 100%
3245
3346 libxml2-wasm:
34- 64 645 ops/s, ±1.22 % | 44.35 % slower
47+ 120 578 ops/s, ±0.12 % | 28 % slower
3548
3649 libxml2-wasm(buffer api):
37- 116 157 ops/s, ±0.67 % | fastest
50+ 167 479 ops/s, ±0.37 % | fastest
3851
3952 libxmljs2:
40- 28 240 ops/s, ±9.20% | 75.69% slower
53+ 78 688 ops/s, ±1.05% | 53.02% slower
54+
55+ @rgrove/parse-xml:
56+ 78 790 ops/s, ±0.19% | 52.96% slower
4157
4258 fast-xml-parser:
43- 18 091 ops/s, ±7.90 % | 84.43 % slower
59+ 52 077 ops/s, ±0.27 % | 68.91 % slower
4460
4561 xmldoc:
46- 14 310 ops/s, ±1.88 % | slowest, 87.68 % slower
62+ 41 125 ops/s, ±0.42 % | slowest, 75.44 % slower
4763
4864Running "fixtures/medium.xml: 35562 bytes" suite...
4965Progress: 100%
5066
5167 libxml2-wasm:
52- 2 706 ops/s, ±0.79 % | 73.57 % slower
68+ 6 013 ops/s, ±0.10 % | 64.87 % slower
5369
5470 libxml2-wasm(buffer api):
55- 10 237 ops/s, ±0.48 % | fastest
71+ 17 114 ops/s, ±0.14 % | fastest
5672
5773 libxmljs2:
58- 4 779 ops/s, ±8.57% | 53.32% slower
74+ 12 414 ops/s, ±1.82% | 27.46% slower
75+
76+ @rgrove/parse-xml:
77+ 5 049 ops/s, ±0.20% | 70.5% slower
5978
6079 fast-xml-parser:
61- 995 ops/s, ±2.09 % | 90.28 % slower
80+ 2 708 ops/s, ±0.46 % | 84.18 % slower
6281
6382 xmldoc:
64- 844 ops/s, ±1.33 % | slowest, 91.76 % slower
83+ 1 912 ops/s, ±0.41 % | slowest, 88.83 % slower
6584
6685Running "fixtures/large.xml: 2337522 bytes" suite...
6786Progress: 100%
6887
6988 libxml2-wasm:
70- 22 ops/s, ±1.29 % | 37.14 % slower
89+ 43 ops/s, ±0.11 % | 30.65 % slower
7190
7291 libxml2-wasm(buffer api):
73- 35 ops/s, ±0.88 % | fastest
92+ 62 ops/s, ±0.27 % | fastest
7493
7594 libxmljs2:
76- 21 ops/s, ±14.92% | 40% slower
95+ 50 ops/s, ±1.41% | 19.35% slower
96+
97+ @rgrove/parse-xml:
98+ 19 ops/s, ±0.93% | 69.35% slower
7799
78100 fast-xml-parser:
79- 6 ops/s, ±5.99 % | 82.86 % slower
101+ 16 ops/s, ±0.37 % | 74.19 % slower
80102
81103 xmldoc:
82- 4 ops/s, ±3.67 % | slowest, 88.57 % slower
104+ 11 ops/s, ±1.13 % | slowest, 82.26 % slower
83105```
0 commit comments