Skip to content

Commit

Permalink
add saxes Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Oct 3, 2018
1 parent 12d15fa commit 3d7e71e
Show file tree
Hide file tree
Showing 12 changed files with 487 additions and 37 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ Features:
By default ltx uses its own parser which is the fastest but doesn't support advanced XML features.
ltx supports third party parsers when such features are needed.

| parser | ops/sec | native | stream |
| parser | ops/sec | JS | stream |
|--------------------------------------------------------------------------------------------|--------:|:------:|:------:|
| [sax-js](https://github.com/isaacs/sax-js) | 99,412 | ||
| [node-xml](https://github.com/dylang/node-xml) | 130,631 || |
| [libxmljs](https://github.com/polotek/libxmljs) | 276,136 || |
| [node-expat](https://github.com/node-xmpp/node-expat) | 322,769 |||
| **[ltx/lib/parsers/ltx](https://github.com/node-xmpp/ltx/blob/master/lib/parsers/ltx.js)** | 641,327 |||

| [sax-js](https://github.com/isaacs/sax-js) | 43,058 | ||
| [libxmljs](https://github.com/polotek/libxmljs) | 56,763 || |
| [saxes](https://github.com/lddubeau/saxes) | 62,246 || |
| [node-xml](https://github.com/dylang/node-xml) | 81,980 |||
| [node-expat](https://github.com/node-xmpp/node-expat) | 72,720 |||
| **[ltx/lib/parsers/ltx](https://github.com/node-xmpp/ltx/blob/master/lib/parsers/ltx.js)** | 490,593 |||

From [ltx/benchmarks/parsers.js](https://github.com/node-xmpp/ltx/blob/master/benchmarks/parsers.js), higher is better.
Node.js v10.11.0 - i5-2520M

## Benchmark

Expand Down
3 changes: 3 additions & 0 deletions benchmarks/data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<foo bar="urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6">
<test>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</test>
</foo>
13 changes: 7 additions & 6 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict'

var readdir = require('fs').readdirSync
var basename = require('path').basename
const suites = [
require('./ltx'),
require('./parsers'),
require('./parse'),
require('./write')
]

readdir(__dirname).forEach(function (file) {
if (file === basename(__filename)) return

var suite = require('./' + file)
suites.forEach(function (suite) {
console.log('suite', suite.name)
suite
.on('cycle', function (event) {
Expand Down
8 changes: 3 additions & 5 deletions benchmarks/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
var benchmark = require('benchmark')
var ltx = require('../index')
var parsers = require('../lib/parsers')
var fs = require('fs')
var path = require('path')

var XML = [
'<message to="foo@bar" from="bar@foo" type="chat" id="foobar">',
'<body>Where there is love there is life.</body>',
'</message>'
].join('')
var XML = fs.readFileSync(path.join(__dirname, 'data.xml'), 'utf8')

var suite = new benchmark.Suite('backends parse')

Expand Down
16 changes: 14 additions & 2 deletions benchmarks/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ var nodeXml = require('node-xml')
var libxml = require('libxmljs')
var expat = require('node-expat')
var sax = require('sax')
// var ltx = require('..')
var saxes = require('saxes')
var LtxSaxParser = require('../lib/parsers/ltx')
var fs = require('fs')
var path = require('path')

var XML = fs.readFileSync(path.join(__dirname, 'data.xml'), 'utf8')

function NodeXmlParser () {
var parser = new nodeXml.SaxParser(function (cb) {})
Expand All @@ -34,6 +38,13 @@ function SaxParser () {
}
this.name = 'sax'
}
function SaxesParser () {
var parser = new saxes.SaxesParser({ fragment: true })
this.parse = function (s) {
parser.write(s).close()
}
this.name = 'saxes'
}
function ExpatParser () {
var parser = new expat.Parser()
this.parse = function (s) {
Expand All @@ -51,6 +62,7 @@ function LtxParser () {

var parsers = [
SaxParser,
SaxesParser,
NodeXmlParser,
LibXmlJsParser,
ExpatParser,
Expand All @@ -64,7 +76,7 @@ var suite = new benchmark.Suite('XML parsers comparison')
parsers.forEach(function (parser) {
parser.parse('<r>')
suite.add(parser.name, function () {
parser.parse('<foo bar="urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6">urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6 urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</foo>')
parser.parse(XML)
})
})

Expand Down
8 changes: 3 additions & 5 deletions benchmarks/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

var benchmark = require('benchmark')
var parsers = require('../lib/parsers')
var fs = require('fs')
var path = require('path')

var XML = [
'<message to="foo@bar" from="bar@foo" type="chat" id="foobar">',
'<body>Where there is love there is life.</body>',
'</message>'
].join('')
var XML = fs.readFileSync(path.join(__dirname, 'data.xml'), 'utf8')

var suite = new benchmark.Suite('backends write')

Expand Down
3 changes: 2 additions & 1 deletion lib/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = [
'node-xml',
'libxmljs',
'node-expat',
'ltx'
'ltx',
'saxes'
].map(function (name) {
return require('./' + name)
})
7 changes: 0 additions & 7 deletions lib/parsers/ltx.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,6 @@ var SaxLtx = module.exports = function SaxLtx () {
recordStart = 0
}
}
/*
var origEmit = this.emit
this.emit = function() {
console.log('ltx', arguments)
origEmit.apply(this, arguments)
}
*/
}
inherits(SaxLtx, EventEmitter)

Expand Down
43 changes: 43 additions & 0 deletions lib/parsers/saxes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

var inherits = require('inherits')
var EventEmitter = require('events').EventEmitter
var saxes = require('saxes')

var SaxSaxesjs = module.exports = function SaxSaxesjs () {
EventEmitter.call(this)
this.parser = new saxes.SaxesParser({ fragment: true })

var that = this
this.parser.onopentag = function (a) {
that.emit('startElement', a.name, a.attributes)
}
this.parser.onclosetag = function (el) {
that.emit('endElement', el.name)
}
this.parser.ontext = function (str) {
that.emit('text', str)
}
this.parser.onend = function () {
that.emit('end')
}
this.parser.onerror = function (e) {
that.emit('error', e)
}
}

inherits(SaxSaxesjs, EventEmitter)

SaxSaxesjs.prototype.write = function (data) {
if (typeof data !== 'string') {
data = data.toString()
}
this.parser.write(data)
}

SaxSaxesjs.prototype.end = function (data) {
if (data) {
this.parser.write(data)
}
this.parser.close()
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
},
"devDependencies": {
"benchmark": "^2.1.3",
"browserify": "^16.2.2",
"browserify": "^16.2.3",
"libxmljs": "^0.19.5",
"microtime": "^2.1.2",
"node-expat": "^2.3.13",
"node-xml": "^1.0.2",
"sax": "^1.2.2",
"saxes": "^3.1.3",
"standard": "^12.0.1",
"vows": "^0.8.1"
}
Expand Down
1 change: 1 addition & 0 deletions test/parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var ltx = require('..')
var parsers = require('../lib/parsers')

parsers.forEach(function (Parser) {
if (Parser.name === 'SaxSaxesjs') return
var parse = function (s) {
return ltx.parse(s, { Parser: Parser })
}
Expand Down
Loading

0 comments on commit 3d7e71e

Please sign in to comment.