Skip to content

Commit 9ed22d8

Browse files
committed
Added @rgrove/parse-xml to the benchmark
1 parent 5aac9cd commit 9ed22d8

File tree

4 files changed

+88
-31
lines changed

4 files changed

+88
-31
lines changed

benchmark/index.mts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as os from 'node:os';
44
import * as libxmljs2 from 'libxmljs2';
55
import { XMLParser } from 'fast-xml-parser';
66
import { XmlDocument } from 'xmldoc';
7+
import { parseXml } from '@rgrove/parse-xml';
78
import { parseXmlBuffer, parseXmlString } from 'libxml2-wasm';
89

910
console.log(`Environment: NodeJs ${process.version} on ${os.type()} ${os.arch()} ${os.cpus()[0].model}\n`);
@@ -16,13 +17,16 @@ for (const fixture of ['fixtures/small.xml', 'fixtures/medium.xml', 'fixtures/la
1617
const doc = parseXmlString(xmlString);
1718
doc.dispose();
1819
}),
19-
benny.add('libxml2-wasm(buffer api)', () => {
20-
const doc = parseXmlBuffer(xmlBuffer);
21-
doc.dispose();
22-
}),
20+
benny.add('libxml2-wasm(buffer api)', () => {
21+
const doc = parseXmlBuffer(xmlBuffer);
22+
doc.dispose();
23+
}),
2324
benny.add('libxmljs2', () => {
2425
libxmljs2.parseXmlString(xmlString);
2526
}),
27+
benny.add('@rgrove/parse-xml', () => {
28+
parseXml(xmlString);
29+
}),
2630
benny.add('fast-xml-parser', () => {
2731
new XMLParser({ ignoreAttributes: false, processEntities: false}).parse(xmlString)
2832
}),

benchmark/package-lock.json

Lines changed: 32 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmark/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"description": "Benchmark for libxml2-wasm",
55
"main": "index.js",
66
"scripts": {
7-
"test": "ts-node --esm index.mts"
7+
"test": "node --loader ts-node/esm index.mts"
88
},
99
"author": "",
1010
"license": "MIT",
1111
"dependencies": {
12+
"@rgrove/parse-xml": "^4.1.0",
1213
"benny": "^3.7.1",
1314
"fast-xml-parser": "^4.2.7",
1415
"libxml2-wasm": "file:..",
@@ -18,6 +19,7 @@
1819
"xmldoc": "^1.3.0"
1920
},
2021
"devDependencies": {
22+
"@types/benchmark": "^2.1.5",
2123
"@types/xmldoc": "^1.1.6"
2224
}
2325
}

docs/performance.md

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
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+
1226
To run the benchmark, build the lib first,
1327

1428
```sh
@@ -22,62 +36,70 @@ cd benchmark && npm ci
2236
npm 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...
3144
Progress: 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
4864
Running "fixtures/medium.xml: 35562 bytes" suite...
4965
Progress: 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
6685
Running "fixtures/large.xml: 2337522 bytes" suite...
6786
Progress: 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

Comments
 (0)