-
Notifications
You must be signed in to change notification settings - Fork 44
/
parsers.js
83 lines (73 loc) · 1.69 KB
/
parsers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* benchmark the parsing speed of the supported backends
* difference with parse.js benchmark is that this doesn't use ltx at all
*/
import fs from "fs";
import benchmark from "benchmark";
import nodeXml from "node-xml";
import libxml from "libxmljs";
import expat from "node-expat";
import sax from "sax";
import saxes from "saxes";
import LtxSaxParser from "../src/parsers/ltx.js";
const XML = fs.readFileSync(new URL("data.xml", import.meta.url), "utf8");
function NodeXmlParser() {
const parser = new nodeXml.SaxParser(() => {});
this.parse = (s) => {
parser.parseString(s);
};
this.name = "node-xml";
}
function LibXmlJsParser() {
const parser = new libxml.SaxPushParser(() => {});
this.parse = (s) => {
parser.push(s, false);
};
this.name = "libxmljs";
}
function SaxParser() {
const parser = sax.parser();
this.parse = (s) => {
parser.write(s).close();
};
this.name = "sax";
}
function SaxesParser() {
const parser = new saxes.SaxesParser({ fragment: true });
this.parse = (s) => {
parser.write(s);
};
this.name = "saxes";
}
function ExpatParser() {
const parser = new expat.Parser();
this.parse = (s) => {
parser.parse(s, false);
};
this.name = "node-expat";
}
function LtxParser() {
const parser = new LtxSaxParser();
this.parse = (s) => {
parser.write(s);
};
this.name = "ltx";
}
const parsers = [
SaxParser,
SaxesParser,
NodeXmlParser,
LibXmlJsParser,
ExpatParser,
LtxParser,
].map((Parser) => {
return new Parser();
});
const suite = new benchmark.Suite("XML parsers comparison");
for (const parser of parsers) {
parser.parse("<r>");
suite.add(parser.name, () => {
parser.parse(XML);
});
}
export default suite;