forked from xmppo/node-expat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.js
71 lines (65 loc) · 1.49 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
'use strict'
var benchmark = require('benchmark')
var nodeXml = require('node-xml')
var libxml = require('libxmljs')
var expat = require('./')
var sax = require('sax')
var LtxSaxParser = require('ltx/lib/parsers/ltx')
function NodeXmlParser () {
var parser = new nodeXml.SaxParser(function (cb) {})
this.parse = function (s) {
parser.parseString(s)
}
this.name = 'node-xml'
}
function LibXmlJsParser () {
var parser = new libxml.SaxPushParser(function (cb) {})
this.parse = function (s) {
parser.push(s, false)
}
this.name = 'libxmljs'
}
function SaxParser () {
var parser = sax.parser()
this.parse = function (s) {
parser.write(s).close()
}
this.name = 'sax'
}
function ExpatParser () {
var parser = new expat.Parser()
this.parse = function (s) {
parser.parse(s, false)
}
this.name = 'node-expat'
}
function LtxParser () {
var parser = new LtxSaxParser()
this.parse = function (s) {
parser.write(s)
}
this.name = 'ltx'
}
var parsers = [
SaxParser,
NodeXmlParser,
LibXmlJsParser,
ExpatParser,
LtxParser
].map(function (Parser) {
return new Parser()
})
var 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})