Skip to content

Commit 9df8953

Browse files
committed
update benchmark test
1 parent eec8477 commit 9df8953

15 files changed

+347
-270
lines changed

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ npm install --save node-html-parser
2020
## Performance
2121

2222
```shell
23-
cheerio :12.0726 ms/file ± 7.31605
24-
parse5 :8.18615 ms/file ± 6.15337
25-
node-html-parser (last release):2.16533 ms/file ± 1.56924
26-
htmlparser :17.0658 ms/file ± 120.901
27-
htmlparser2 :2.62695 ms/file ± 4.17579
28-
node-html-parser:2.14907 ms/file ± 1.66632
29-
html-parser :24.6505 ms/file ± 18.9996
30-
htmljs-parser :5.81797 ms/file ± 6.55537
31-
html-dom-parser :2.52265 ms/file ± 3.54858
32-
html5parser :2.01144 ms/file ± 2.53570
33-
high5 :3.91342 ms/file ± 2.65563
23+
html-parser :24.2329 ms/file ± 18.8092
24+
htmljs-parser :4.78952 ms/file ± 5.50403
25+
html-dom-parser :2.19594 ms/file ± 3.07470
26+
html5parser :1.72007 ms/file ± 2.22713
27+
cheerio :12.2220 ms/file ± 8.14063
28+
parse5 :6.77691 ms/file ± 4.12002
29+
htmlparser2 :2.33526 ms/file ± 3.43847
30+
htmlparser :17.6260 ms/file ± 122.314
31+
high5 :3.85676 ms/file ± 2.48878
32+
node-html-parser:2.04585 ms/file ± 1.23787
33+
node-html-parser (last release):2.00236 ms/file ± 1.22263
3434
```
3535

3636
Tested with [htmlparser-benchmark](https://github.com/AndreasMadsen/htmlparser-benchmark).

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"prepare": "cd test && yarn install"
3232
},
3333
"keywords": [
34-
"parser",
3534
"html",
35+
"parser",
3636
"nodejs",
3737
"typescript"
3838
],
@@ -114,4 +114,4 @@
114114
},
115115
"homepage": "https://github.com/taoqf/node-fast-html-parser",
116116
"sideEffects": false
117-
}
117+
}

test/benchmark/compare-cheerio.mjs

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import benchmark from 'htmlparser-benchmark';
21
import { load } from "cheerio";
2+
import benchmark from 'htmlparser-benchmark';
33

4-
var bench = benchmark(function (html, callback) {
5-
// parse(html);
6-
const $ = load(html);
7-
$.html();
8-
callback();
9-
});
4+
export default function cheerio() {
5+
return new Promise((res) => {
6+
var bench = benchmark(function (html, callback) {
7+
// parse(html);
8+
const $ = load(html);
9+
$.html();
10+
callback();
11+
});
1012

11-
bench.on('progress', function (key) {
12-
// console.log('finished parsing ' + key + '.html');
13-
});
13+
bench.on('progress', function (key) {
14+
// console.log('finished parsing ' + key + '.html');
15+
});
1416

15-
bench.on('result', function (stat) {
16-
console.log('cheerio :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
17-
});
17+
bench.on('result', function (stat) {
18+
console.log('cheerio :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
19+
res();
20+
});
21+
});
22+
}

test/benchmark/compare-high5.mjs

+51-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import benchmark from 'htmlparser-benchmark';
21
import high5 from "high5";
2+
import benchmark from 'htmlparser-benchmark';
33

44
const Parser = high5;
55

@@ -9,50 +9,54 @@ var token = [],
99

1010
function noop() { }
1111

12-
13-
14-
var bench = benchmark(function (html, callback) {
15-
var parser = new Parser({
16-
token: token,
17-
onopentagname: function (n) {
18-
attribs = {};
19-
tag = ["StartTag", n, attribs];
20-
},
21-
onclosetag: function (n) {
22-
token.push(["EndTag", n]);
23-
},
24-
ontext: function (t) {
25-
token.push(["Character", t]);
26-
},
27-
oncomment: function (t) {
28-
token.push(["Comment", t]);
29-
},
30-
onattribute: function (n, v) {
31-
if (!(n in attribs)) attribs[n] = v;
32-
},
33-
onopentagend: function () {
34-
token.push(tag);
35-
tag = attribs = null;
36-
},
37-
onselfclosingtag: function () {
38-
tag.push(true);
39-
token.push(tag);
40-
tag = attribs = null;
41-
},
42-
ondoctype: function (name, publicIdent, systemIdent, normalMode) {
43-
token.push(["DOCTYPE", name, publicIdent, systemIdent, normalMode]);
44-
},
45-
oncommentend: noop,
46-
onend: callback,
47-
onerror: callback
12+
export default function high5parse() {
13+
return new Promise((res) => {
14+
15+
var bench = benchmark(function (html, callback) {
16+
var parser = new Parser({
17+
token: token,
18+
onopentagname: function (n) {
19+
attribs = {};
20+
tag = ["StartTag", n, attribs];
21+
},
22+
onclosetag: function (n) {
23+
token.push(["EndTag", n]);
24+
},
25+
ontext: function (t) {
26+
token.push(["Character", t]);
27+
},
28+
oncomment: function (t) {
29+
token.push(["Comment", t]);
30+
},
31+
onattribute: function (n, v) {
32+
if (!(n in attribs)) attribs[n] = v;
33+
},
34+
onopentagend: function () {
35+
token.push(tag);
36+
tag = attribs = null;
37+
},
38+
onselfclosingtag: function () {
39+
tag.push(true);
40+
token.push(tag);
41+
tag = attribs = null;
42+
},
43+
ondoctype: function (name, publicIdent, systemIdent, normalMode) {
44+
token.push(["DOCTYPE", name, publicIdent, systemIdent, normalMode]);
45+
},
46+
oncommentend: noop,
47+
onend: callback,
48+
onerror: callback
49+
});
50+
parser.end(html);
51+
});
52+
53+
bench.on('progress', function (key) {
54+
// console.log('finished parsing ' + key + '.html');
55+
});
56+
57+
bench.on('result', function (stat) {
58+
console.log('high5 :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
59+
res();
60+
});
4861
});
49-
parser.end(html);
50-
});
51-
52-
bench.on('progress', function (key) {
53-
// console.log('finished parsing ' + key + '.html');
54-
});
55-
56-
bench.on('result', function (stat) {
57-
console.log('high5 :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
58-
});
62+
}
+16-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import benchmark from 'htmlparser-benchmark';
21
import parse from "html-dom-parser";
2+
import benchmark from 'htmlparser-benchmark';
33

4-
var bench = benchmark(function (html, callback) {
5-
parse(html);
6-
callback();
7-
});
4+
export default function high5() {
5+
return new Promise((res) => {
6+
var bench = benchmark(function (html, callback) {
7+
parse(html);
8+
callback();
9+
});
810

9-
bench.on('progress', function (key) {
10-
// console.log('finished parsing ' + key + '.html');
11-
});
11+
bench.on('progress', function (key) {
12+
// console.log('finished parsing ' + key + '.html');
13+
});
1214

13-
bench.on('result', function (stat) {
14-
console.log('html-dom-parser :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
15-
});
15+
bench.on('result', function (stat) {
16+
console.log('html-dom-parser :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
17+
res();
18+
});
19+
});
20+
}
+17-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
import benchmark from 'htmlparser-benchmark';
21
import htmlParser from "html-parser";
2+
import benchmark from 'htmlparser-benchmark';
3+
34

4-
var bench = benchmark(function (html, callback) {
5-
htmlParser.parse(html);
6-
callback();
7-
});
5+
export default function parsehtml() {
6+
return new Promise((res) => {
7+
var bench = benchmark(function (html, callback) {
8+
htmlParser.parse(html);
9+
callback();
10+
});
811

9-
bench.on('progress', function (key) {
10-
// console.log('finished parsing ' + key + '.html');
11-
});
12+
bench.on('progress', function (key) {
13+
// console.log('finished parsing ' + key + '.html');
14+
});
1215

13-
bench.on('result', function (stat) {
14-
console.log('html-parser :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
15-
});
16+
bench.on('result', function (stat) {
17+
console.log('html-parser :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
18+
res();
19+
});
20+
});
21+
}
+16-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import benchmark from 'htmlparser-benchmark';
21
import { parse } from "html5parser";
2+
import benchmark from 'htmlparser-benchmark';
33

4-
var bench = benchmark(function (html, callback) {
5-
parse(html);
6-
callback();
7-
});
4+
export default function high5parser() {
5+
return new Promise((res) => {
6+
var bench = benchmark(function (html, callback) {
7+
parse(html);
8+
callback();
9+
});
810

9-
bench.on('progress', function (key) {
10-
// console.log('finished parsing ' + key + '.html');
11-
});
11+
bench.on('progress', function (key) {
12+
// console.log('finished parsing ' + key + '.html');
13+
});
1214

13-
bench.on('result', function (stat) {
14-
console.log('html5parser :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
15-
});
15+
bench.on('result', function (stat) {
16+
console.log('html5parser :' + stat.mean().toPrecision(6) + ' ms/file ± ' + stat.sd().toPrecision(6));
17+
res();
18+
});
19+
});
20+
}

0 commit comments

Comments
 (0)