Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of parsing #126

Merged
merged 2 commits into from
Sep 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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="baz">quux</foo>')
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>')
})
})

Expand Down
27 changes: 26 additions & 1 deletion lib/parsers/ltx.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var SaxLtx = module.exports = function SaxLtx () {
var endTag
var selfClosing
var attrQuote
var attrQuoteChar
var recordStart = 0
var attrName

Expand Down Expand Up @@ -54,13 +55,36 @@ var SaxLtx = module.exports = function SaxLtx () {

function endRecording () {
if (typeof recordStart === 'number') {
var recorded = data.slice(recordStart, pos)
var recorded = data.substring(recordStart, pos)
recordStart = undefined
return recorded
}
}

for (; pos < data.length; pos++) {
if (state === STATE_TEXT) {
// if we're looping through text, fast-forward using indexOf to
// the next '<' character
const lt = data.indexOf('<', pos)
if (lt !== -1 && pos !== lt) {
pos = lt
}
} else if (state === STATE_ATTR_VALUE) {
// if we're looping through an attribute, fast-forward using
// indexOf to the next end quote character
const quot = data.indexOf(attrQuoteChar, pos)
if (quot !== -1) {
pos = quot
}
} else if (state === STATE_IGNORE_COMMENT) {
// if we're looping through a comment, fast-forward using
// indexOf to the first end-comment character
const endcomment = data.indexOf('-->', pos)
if (endcomment !== -1) {
pos = endcomment + 2 // target the '>' character
}
}

var c = data.charCodeAt(pos)
switch (state) {
case STATE_TEXT:
Expand Down Expand Up @@ -153,6 +177,7 @@ var SaxLtx = module.exports = function SaxLtx () {
case STATE_ATTR_QUOT:
if (c === 34 /* " */ || c === 39 /* ' */) {
attrQuote = c
attrQuoteChar = c === 34 ? '"' : "'"
state = STATE_ATTR_VALUE
recordStart = pos + 1
}
Expand Down