Skip to content

Commit

Permalink
Split internal 'is' function into regex vs charclass usage.
Browse files Browse the repository at this point in the history
Prevents costly Object.prototype.toString call.
Switch from !!regex.match to regex.test for ~10% improvement.
  • Loading branch information
lovell committed Jan 17, 2017
1 parent 7a3ad9d commit b9d12be
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,16 @@
}, {})
}

function isRegExp (c) {
return Object.prototype.toString.call(c) === '[object RegExp]'
function isMatch (regex, c) {
return regex.test(c)
}

function is (charclass, c) {
return isRegExp(charclass) ? !!c.match(charclass) : charclass[c]
return charclass[c]
}

function notMatch (regex, c) {
return !isMatch(regex, c)
}

function not (charclass, c) {
Expand Down Expand Up @@ -1080,7 +1084,7 @@
parser.sgmlDecl = ''
} else if (is(whitespace, c)) {
// wait for it...
} else if (is(nameStart, c)) {
} else if (isMatch(nameStart, c)) {
parser.state = S.OPEN_TAG
parser.tagName = c
} else if (c === '/') {
Expand Down Expand Up @@ -1283,7 +1287,7 @@
continue

case S.OPEN_TAG:
if (is(nameBody, c)) {
if (isMatch(nameBody, c)) {
parser.tagName += c
} else {
newTag(parser)
Expand Down Expand Up @@ -1318,7 +1322,7 @@
openTag(parser)
} else if (c === '/') {
parser.state = S.OPEN_TAG_SLASH
} else if (is(nameStart, c)) {
} else if (isMatch(nameStart, c)) {
parser.attribName = c
parser.attribValue = ''
parser.state = S.ATTRIB_NAME
Expand All @@ -1337,7 +1341,7 @@
openTag(parser)
} else if (is(whitespace, c)) {
parser.state = S.ATTRIB_NAME_SAW_WHITE
} else if (is(nameBody, c)) {
} else if (isMatch(nameBody, c)) {
parser.attribName += c
} else {
strictFail(parser, 'Invalid attribute name')
Expand All @@ -1360,7 +1364,7 @@
parser.attribName = ''
if (c === '>') {
openTag(parser)
} else if (is(nameStart, c)) {
} else if (isMatch(nameStart, c)) {
parser.attribName = c
parser.state = S.ATTRIB_NAME
} else {
Expand Down Expand Up @@ -1404,7 +1408,7 @@
openTag(parser)
} else if (c === '/') {
parser.state = S.OPEN_TAG_SLASH
} else if (is(nameStart, c)) {
} else if (isMatch(nameStart, c)) {
strictFail(parser, 'No whitespace between attributes')
parser.attribName = c
parser.attribValue = ''
Expand Down Expand Up @@ -1435,7 +1439,7 @@
if (!parser.tagName) {
if (is(whitespace, c)) {
continue
} else if (not(nameStart, c)) {
} else if (notMatch(nameStart, c)) {
if (parser.script) {
parser.script += '</' + c
parser.state = S.SCRIPT
Expand All @@ -1447,7 +1451,7 @@
}
} else if (c === '>') {
closeTag(parser)
} else if (is(nameBody, c)) {
} else if (isMatch(nameBody, c)) {
parser.tagName += c
} else if (parser.script) {
parser.script += '</' + parser.tagName
Expand Down Expand Up @@ -1498,7 +1502,7 @@
parser[buffer] += parseEntity(parser)
parser.entity = ''
parser.state = returnState
} else if (is(parser.entity.length ? entityBody : entityStart, c)) {
} else if (isMatch(parser.entity.length ? entityBody : entityStart, c)) {
parser.entity += c
} else {
strictFail(parser, 'Invalid character in entity name')
Expand Down

0 comments on commit b9d12be

Please sign in to comment.