-
Notifications
You must be signed in to change notification settings - Fork 28
/
benchmark.js
51 lines (44 loc) · 1.25 KB
/
benchmark.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
/*
Based on the http://mootools.net/slickspeed/ benchmarks. Uses the same sample document
but only runs those tests where the CSS syntax used is supported by soupselect
*/
var select = require('soupselect').select,
htmlparser = require("htmlparser"),
fs = require('fs');
var html = fs.readFileSync('testdata/benchmark.html', 'utf-8');
var selectors = [
'body',
'div',
'body div',
'div p',
'div p a',
'.note',
'div.example',
'ul .tocline2',
'#title',
'h1#title',
'div #title',
'ul.toc li.tocline2',
'div[class]',
'div[class=example]',
'div[class^=exa]',
'div[class$=mple]',
'div[class*=e]',
'div[class|=dialog]',
'div[class!=made_up]',
'div[class~=example]',
];
selectors.forEach(function(selector) {
var handler = new htmlparser.DefaultHandler(function(err, dom) {
if (err) {
console.error("Error: " + err);
} else {
var start = new Date().getTime();
var els = select(dom, selector);
var elapsed = new Date().getTime() - start;
sys.puts(selector + " : " + elapsed + "ms, " + els.length + " elements");
}
});
var parser = new htmlparser.Parser(handler);
parser.parseComplete(html);
});