Skip to content

Commit

Permalink
perf: inline closeText
Browse files Browse the repository at this point in the history
With the recent changes to the code base, we've reduced the number of locations
where closeText was called to 3. Moreover, inlining closeText allows for some
further optimizations. (We can sometimes avoid concatenating strings.)
  • Loading branch information
lddubeau committed Oct 4, 2019
1 parent d80a210 commit 1c8df1a
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,15 +1055,21 @@ class SaxesParser {
scanLoop:
while (true) {
switch (this.getCode()) {
case LESS:
case LESS: {
this.state = S_OPEN_WAKA;
this.text += chunk.slice(start, this.prevI);
if (this.text.length !== 0) {
this.closeText();
const { text } = this;
const slice = chunk.slice(start, this.prevI);
if (text.length !== 0) {
this.ontext(text + slice);
this.text = "";
}
else if (slice.length !== 0) {
this.ontext(slice);
}
forbiddenState = FORBIDDEN_START;
// eslint-disable-next-line no-labels
break scanLoop;
}
case AMP:
this.state = S_ENTITY;
this.entityReturnState = S_TEXT;
Expand Down Expand Up @@ -1121,14 +1127,20 @@ class SaxesParser {
while (true) {
const code = this.getCode();
switch (code) {
case LESS:
case LESS: {
this.state = S_OPEN_WAKA;
this.text += chunk.slice(start, this.prevI);
if (this.text.length !== 0) {
this.closeText();
const { text } = this;
const slice = chunk.slice(start, this.prevI);
if (text.length !== 0) {
this.ontext(text + slice);
this.text = "";
}
else if (slice.length !== 0) {
this.ontext(slice);
}
// eslint-disable-next-line no-labels
break outRootLoop;
}
case AMP:
this.state = S_ENTITY;
this.entityReturnState = S_TEXT;
Expand Down Expand Up @@ -2049,25 +2061,17 @@ class SaxesParser {
(this.state !== S_TEXT)) {
this.fail("unexpected end.");
}
if (this.text.length !== 0) {
this.closeText();
const { text } = this;
if (text.length !== 0) {
this.ontext(text);
this.text = "";
}
this.closed = true;
this.onend();
this._init(this.opt);
return this;
}

/**
* If there's text to emit ``ontext``, emit it.
*
* @private
*/
closeText() {
this.ontext(this.text);
this.text = "";
}

/**
* Resolve a namespace prefix.
*
Expand Down

0 comments on commit 1c8df1a

Please sign in to comment.