Skip to content

Commit

Permalink
perf: use slice rather than substring
Browse files Browse the repository at this point in the history
It is a tiny bit faster.
  • Loading branch information
lddubeau committed Sep 12, 2019
1 parent 8cfd5ed commit c1fed89
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ class SaxesParser {
// until we get the next chunk or the end of the stream. So save it for
// later.
limit--;
chunk = chunk.substring(0, limit);
chunk = chunk.slice(0, limit);
this.trailingCR = true;
}

Expand Down Expand Up @@ -792,7 +792,7 @@ class SaxesParser {
}

const { i } = this;
this[buffer] += `${chunk.substring(start, i - (this.skipTwo ? 2 : 1))}\n`;
this[buffer] += `${chunk.slice(start, i - (this.skipTwo ? 2 : 1))}\n`;
return i;
}

Expand All @@ -819,12 +819,12 @@ class SaxesParser {
start = this.handleEOL(buffer, chunk, start);
}
else if (c === undefined) {
this[buffer] += chunk.substring(start);
this[buffer] += chunk.slice(start);
return undefined;
}

if (chars.includes(c)) {
this[buffer] += chunk.substring(start, this.i - (c <= 0xFFFF ? 1 : 2));
this[buffer] += chunk.slice(start, this.i - (c <= 0xFFFF ? 1 : 2));
return c;
}
}
Expand All @@ -851,12 +851,12 @@ class SaxesParser {
start = this.handleEOL(buffer, chunk, start);
}
else if (c === undefined) {
this[buffer] += chunk.substring(start);
this[buffer] += chunk.slice(start);
return false;
}

if (c === char) {
this[buffer] += chunk.substring(start, this.i - (c <= 0xFFFF ? 1 : 2));
this[buffer] += chunk.slice(start, this.i - (c <= 0xFFFF ? 1 : 2));
return true;
}
}
Expand All @@ -876,13 +876,13 @@ class SaxesParser {
while (true) {
const c = this.getCode();
if (c === undefined) {
this.name += chunk.substring(start);
this.name += chunk.slice(start);
return undefined;
}

// NL is not a name char so we don't have to test specifically for it.
if (!isNameChar(c)) {
this.name += chunk.substring(start, this.i - (c <= 0xFFFF ? 1 : 2));
this.name += chunk.slice(start, this.i - (c <= 0xFFFF ? 1 : 2));
return c;
}
}
Expand All @@ -904,14 +904,14 @@ class SaxesParser {
while (true) {
const c = this.getCode();
if (c === undefined) {
this[buffer] += chunk.substring(start);
this[buffer] += chunk.slice(start);
return undefined;
}

// NL cannot satisfy this.nameCheck so we don't have to test
// specifically for it.
if (!this.nameCheck(c)) {
this[buffer] += chunk.substring(start, this.i - (c <= 0xFFFF ? 1 : 2));
this[buffer] += chunk.slice(start, this.i - (c <= 0xFFFF ? 1 : 2));
return c;
}
}
Expand Down Expand Up @@ -1032,14 +1032,14 @@ class SaxesParser {
switch (this.getCode()) {
case LESS:
this.state = S_OPEN_WAKA;
this.text += chunk.substring(start, this.i - 1);
this.text += chunk.slice(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;
this.text += chunk.substring(start, this.i - 1);
this.text += chunk.slice(start, this.i - 1);
forbiddenState = FORBIDDEN_START;
// eslint-disable-next-line no-labels
break scanLoop;
Expand Down Expand Up @@ -1068,7 +1068,7 @@ class SaxesParser {
forbiddenState = FORBIDDEN_START;
break;
case undefined:
this.text += chunk.substring(start);
this.text += chunk.slice(start);
// eslint-disable-next-line no-labels
break scanLoop;
default:
Expand All @@ -1094,13 +1094,13 @@ class SaxesParser {
switch (code) {
case LESS:
this.state = S_OPEN_WAKA;
this.text += chunk.substring(start, this.i - 1);
this.text += chunk.slice(start, this.i - 1);
// eslint-disable-next-line no-labels
break outRootLoop;
case AMP:
this.state = S_ENTITY;
this.entityReturnState = S_TEXT;
this.text += chunk.substring(start, this.i - 1);
this.text += chunk.slice(start, this.i - 1);
nonSpace = true;
// eslint-disable-next-line no-labels
break outRootLoop;
Expand All @@ -1109,7 +1109,7 @@ class SaxesParser {
// eslint-disable-next-line no-labels
break;
case undefined:
this.text += chunk.substring(start);
this.text += chunk.slice(start);
// eslint-disable-next-line no-labels
break outRootLoop;
default:
Expand Down Expand Up @@ -1857,15 +1857,15 @@ class SaxesParser {
while (true) {
const code = this.getCode();
if (code === undefined) {
this.text += chunk.substring(start);
this.text += chunk.slice(start);
return;
}

if (code === NL) {
start = this.handleEOL("text", chunk, start);
}
else if (code === q || code === AMP || code === LESS) {
const slice = chunk.substring(start, this.i - 1);
const slice = chunk.slice(start, this.i - 1);
switch (code) {
case q:
this.pushAttrib(this.name, this.text + slice);
Expand Down Expand Up @@ -2062,8 +2062,8 @@ class SaxesParser {
return { prefix: "", local: name };
}

const local = name.substring(colon + 1);
const prefix = name.substring(0, colon);
const local = name.slice(colon + 1);
const prefix = name.slice(0, colon);
if (prefix === "" || local === "" || local.includes(":")) {
this.fail(`malformed name: ${name}.`);
}
Expand Down

0 comments on commit c1fed89

Please sign in to comment.