-
Notifications
You must be signed in to change notification settings - Fork 97
/
benchmark.js
80 lines (72 loc) · 1.67 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
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
'use strict'
const benchmark = require('benchmark')
const nodeXml = require('node-xml')
let libxml = null
const expat = require('./')
const sax = require('sax')
const LtxSaxParser = require('ltx/lib/parsers/ltx')
try {
libxml = require('libxmljs')
} catch (err) {
console.error('Cannot load libxmljs, please install it manually:', err)
}
function NodeXmlParser () {
const parser = new nodeXml.SaxParser(function (cb) {})
this.parse = function (s) {
parser.parseString(s)
}
this.name = 'node-xml'
}
function LibXmlJsParser () {
const parser = new libxml.SaxPushParser(function (cb) {})
this.parse = function (s) {
parser.push(s, false)
}
this.name = 'libxmljs'
}
function SaxParser () {
const parser = sax.parser()
this.parse = function (s) {
parser.write(s).close()
}
this.name = 'sax'
}
function ExpatParser () {
const parser = new expat.Parser()
this.parse = function (s) {
parser.parse(s, false)
}
this.name = 'node-expat'
}
function LtxParser () {
const parser = new LtxSaxParser()
this.parse = function (s) {
parser.write(s)
}
this.name = 'ltx'
}
const parsers = [
SaxParser,
NodeXmlParser,
ExpatParser,
LtxParser
].map(function (Parser) {
return new Parser()
})
if (libxml) {
parsers.push(new LibXmlJsParser())
}
const suite = new benchmark.Suite('parse')
parsers.forEach(function (parser) {
parser.parse('<r>')
suite.add(parser.name, function () {
parser.parse('<foo bar="baz">quux</foo>')
})
})
suite.on('cycle', function (event) {
console.log(event.target.toString())
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })