Skip to content

Commit

Permalink
perf: improve performance of text handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Sep 12, 2019
1 parent b19edfa commit 9c13099
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,22 +948,28 @@ class SaxesParser {
//
const { chunk, limit, i: start } = this;
let { forbiddenState } = this;
let c;
// eslint-disable-next-line no-labels, no-restricted-syntax
scanLoop:
while (this.i < limit) {
// eslint-disable-next-line no-constant-condition
while (true) {
if (this.i >= limit) {
this.text += chunk.substring(start);
break;
}
const code = this.getCode();
switch (code) {
case LESS:
this.state = S_OPEN_WAKA;
c = code;
// This is faster than adding codepoints one by one.
this.text += chunk.substring(start, this.i - 1);
forbiddenState = FORBIDDEN_START;
// eslint-disable-next-line no-labels
break scanLoop;
case AMP:
this.state = S_ENTITY;
this.entityReturnState = S_TEXT;
c = code;
// This is faster than adding codepoints one by one.
this.text += chunk.substring(start, this.i - 1);
forbiddenState = FORBIDDEN_START;
// eslint-disable-next-line no-labels
break scanLoop;
Expand Down Expand Up @@ -992,11 +998,6 @@ class SaxesParser {
}
}
this.forbiddenState = forbiddenState;

// This is faster than adding codepoints one by one.
this.text += chunk.substring(start,
c === undefined ? undefined :
(this.i - (c <= 0xFFFF ? 1 : 2)));
}

/** @private */
Expand All @@ -1012,21 +1013,27 @@ class SaxesParser {
//
const { chunk, limit, i: start } = this;
let nonSpace = false;
let c;
// eslint-disable-next-line no-labels, no-restricted-syntax
outRootLoop:
while (this.i < limit) {
// eslint-disable-next-line no-constant-condition
while (true) {
if (this.i >= limit) {
this.text += chunk.substring(start);
break;
}
const code = this.getCode();
switch (code) {
case LESS:
this.state = S_OPEN_WAKA;
c = code;
// This is faster than adding codepoints one by one.
this.text += chunk.substring(start, this.i - 1);
// eslint-disable-next-line no-labels
break outRootLoop;
case AMP:
this.state = S_ENTITY;
this.entityReturnState = S_TEXT;
c = code;
// This is faster than adding codepoints one by one.
this.text += chunk.substring(start, this.i - 1);
nonSpace = true;
// eslint-disable-next-line no-labels
break outRootLoop;
Expand All @@ -1037,11 +1044,6 @@ class SaxesParser {
}
}

// This is faster than adding codepoints one by one.
this.text += chunk.substring(start,
c === undefined ? undefined :
(this.i - (c <= 0xFFFF ? 1 : 2)));

if (!nonSpace) {
return;
}
Expand Down Expand Up @@ -1786,8 +1788,7 @@ class SaxesParser {
const code = this.getCode();
if (code === q || code === AMP || code === LESS) {
// This is faster than adding codepoints one by one.
const slice = chunk.substring(start,
this.i - (code <= 0xFFFF ? 1 : 2));
const slice = chunk.substring(start, this.i - 1);
switch (code) {
case q:
this.pushAttrib(this.name, this.text + slice);
Expand Down

0 comments on commit 9c13099

Please sign in to comment.