Skip to content

Commit

Permalink
perf: dump isNaN; it is very costly
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Oct 3, 2019
1 parent 8fd0a78 commit 7d97e1a
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,13 +632,18 @@ class SaxesParser {
getCode10() {
const { chunk, i } = this;
this.prevI = i;
// Yes, we do this instead of doing this.i++. Doing it this way, we do not
// read this.i again, which is a bit faster.
this.i = i + 1;

if (i >= chunk.length) {
return undefined;
}

// Using charCodeAt and handling the surrogates ourselves is faster
// than using codePointAt.
const code = chunk.charCodeAt(i);

// Yes, we do this instead of doing this.i++. Doing it this way, we do not
// read this.i again, which is a bit faster.
this.i = i + 1;
if (code < 0xD800) {
if (code >= SPACE || code === TAB) {
return code;
Expand Down Expand Up @@ -679,11 +684,6 @@ class SaxesParser {
return code;
}

// eslint-disable-next-line no-restricted-globals
if (isNaN(code)) {
return undefined;
}

const final = 0x10000 + ((code - 0xD800) * 0x400) +
(chunk.charCodeAt(i + 1) - 0xDC00);
this.i = i + 2;
Expand Down Expand Up @@ -711,13 +711,18 @@ class SaxesParser {
getCode11() {
const { chunk, i } = this;
this.prevI = i;
// Yes, we do this instead of doing this.i++. Doing it this way, we do not
// read this.i again, which is a bit faster.
this.i = i + 1;

if (i >= chunk.length) {
return undefined;
}

// Using charCodeAt and handling the surrogates ourselves is faster
// than using codePointAt.
const code = chunk.charCodeAt(i);

// Yes, we do this instead of doing this.i++. Doing it this way, we do not
// read this.i again, which is a bit faster.
this.i = i + 1;
if (code < 0xD800) {
if ((code > 0x1F && code < 0x7F) || (code > 0x9F && code !== LS) ||
code === TAB) {
Expand Down Expand Up @@ -761,11 +766,6 @@ class SaxesParser {
return code;
}

// eslint-disable-next-line no-restricted-globals
if (isNaN(code)) {
return undefined;
}

const final = 0x10000 + ((code - 0xD800) * 0x400) +
(chunk.charCodeAt(i + 1) - 0xDC00);
this.i = i + 2;
Expand Down

0 comments on commit 7d97e1a

Please sign in to comment.