Skip to content

Commit

Permalink
perf: use strings for the general states
Browse files Browse the repository at this point in the history
This allows dropping the ``STATE_TO_METHOD_NAME`` array and having to perform
lookups. Better performance.
  • Loading branch information
lddubeau committed Aug 14, 2018
1 parent c7e36bf commit 3869908
Showing 1 changed file with 37 additions and 66 deletions.
103 changes: 37 additions & 66 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,42 @@ const XML_ENTITIES = {
apos: "'",
};

const S_BEGIN_WHITESPACE = 0; // leading whitespace
const S_TEXT = 1; // general stuff
const S_ENTITY = 2; // &amp and such.
const S_OPEN_WAKA = 3; // <
const S_OPEN_WAKA_BANG = 4; // <!...
const S_DOCTYPE = 5; // <!DOCTYPE
const S_DOCTYPE_QUOTED = 6; // <!DOCTYPE "//blah
const S_DOCTYPE_DTD = 7; // <!DOCTYPE "//blah" [ ...
const S_DOCTYPE_DTD_QUOTED = 8; // <!DOCTYPE "//blah" [ "foo
const S_COMMENT = 9; // <!--
const S_COMMENT_ENDING = 10; // <!-- blah -
const S_COMMENT_ENDED = 11; // <!-- blah --
const S_CDATA = 12; // <![CDATA[ something
const S_CDATA_ENDING = 13; // ]
const S_CDATA_ENDING_2 = 14; // ]]
const S_PI = 15; // <?hi
const S_PI_BODY = 16; // <?hi there
const S_PI_ENDING = 17; // <?hi "there" ?
const S_OPEN_TAG = 18; // <strong
const S_OPEN_TAG_SLASH = 19; // <strong /
const S_ATTRIB = 20; // <a
const S_ATTRIB_NAME = 21; // <a foo
const S_ATTRIB_NAME_SAW_WHITE = 22; // <a foo _
const S_ATTRIB_VALUE = 23; // <a foo=
const S_ATTRIB_VALUE_QUOTED = 24; // <a foo="bar
const S_ATTRIB_VALUE_CLOSED = 25; // <a foo="bar"
const S_ATTRIB_VALUE_UNQUOTED = 26; // <a foo=bar
const S_CLOSE_TAG = 27; // </a
const S_CLOSE_TAG_SAW_WHITE = 28; // </a >
const S_XML_DECL_NAME_START = 29; // <?xml
const S_XML_DECL_NAME = 30; // <?xml foo
const S_XML_DECL_EQ = 31; // <?xml foo=
const S_XML_DECL_VALUE_START = 32; // <?xml foo=
const S_XML_DECL_VALUE = 33; // <?xml foo="bar"

const STATE_TO_METHOD_NAME = new Array(S_CLOSE_TAG_SAW_WHITE + 1);
STATE_TO_METHOD_NAME[S_BEGIN_WHITESPACE] = "sBeginWhitespace";
STATE_TO_METHOD_NAME[S_TEXT] = "sText";
STATE_TO_METHOD_NAME[S_ENTITY] = "sEntity";
STATE_TO_METHOD_NAME[S_OPEN_WAKA] = "sOpenWaka";
STATE_TO_METHOD_NAME[S_OPEN_WAKA_BANG] = "sOpenWakaBang";
STATE_TO_METHOD_NAME[S_DOCTYPE] = "sDoctype";
STATE_TO_METHOD_NAME[S_DOCTYPE_QUOTED] = "sDoctypeQuoted";
STATE_TO_METHOD_NAME[S_DOCTYPE_DTD] = "sDoctypeDTD";
STATE_TO_METHOD_NAME[S_DOCTYPE_DTD_QUOTED] = "sDoctypeDTDQuoted";
STATE_TO_METHOD_NAME[S_COMMENT] = "sComment";
STATE_TO_METHOD_NAME[S_COMMENT_ENDING] = "sCommentEnding";
STATE_TO_METHOD_NAME[S_COMMENT_ENDED] = "sCommentEnded";
STATE_TO_METHOD_NAME[S_CDATA] = "sCData";
STATE_TO_METHOD_NAME[S_CDATA_ENDING] = "sCDataEnding";
STATE_TO_METHOD_NAME[S_CDATA_ENDING_2] = "sCDataEnding2";
STATE_TO_METHOD_NAME[S_PI] = "sPI";
STATE_TO_METHOD_NAME[S_PI_BODY] = "sPIBody";
STATE_TO_METHOD_NAME[S_PI_ENDING] = "sPIEnding";
STATE_TO_METHOD_NAME[S_OPEN_TAG] = "sOpenTag";
STATE_TO_METHOD_NAME[S_OPEN_TAG_SLASH] = "sOpenTagSlash";
STATE_TO_METHOD_NAME[S_ATTRIB] = "sAttrib";
STATE_TO_METHOD_NAME[S_ATTRIB_NAME] = "sAttribName";
STATE_TO_METHOD_NAME[S_ATTRIB_NAME_SAW_WHITE] = "sAttribNameSawWhite";
STATE_TO_METHOD_NAME[S_ATTRIB_VALUE] = "sAttribValue";
STATE_TO_METHOD_NAME[S_ATTRIB_VALUE_QUOTED] = "sAttribValueQuoted";
STATE_TO_METHOD_NAME[S_ATTRIB_VALUE_CLOSED] = "sAttribValueClosed";
STATE_TO_METHOD_NAME[S_ATTRIB_VALUE_UNQUOTED] = "sAttribValueUnquoted";
STATE_TO_METHOD_NAME[S_CLOSE_TAG] = "sCloseTag";
STATE_TO_METHOD_NAME[S_CLOSE_TAG_SAW_WHITE] = "sCloseTagSawWhite";
const S_BEGIN_WHITESPACE = "sBeginWhitespace"; // leading whitespace
const S_TEXT = "sText"; // general stuff
const S_ENTITY = "sEntity"; // &amp and such.
const S_OPEN_WAKA = "sOpenWaka"; // <
const S_OPEN_WAKA_BANG = "sOpenWakaBang"; // <!...
const S_DOCTYPE = "sDoctype"; // <!DOCTYPE
const S_DOCTYPE_QUOTED = "sDoctypeQuoted"; // <!DOCTYPE "//blah
const S_DOCTYPE_DTD = "sDoctypeDTD"; // <!DOCTYPE "//blah" [ ...
const S_DOCTYPE_DTD_QUOTED = "sDoctypeDTDQuoted"; // <!DOCTYPE "//blah" [ "foo
const S_COMMENT = "sComment"; // <!--
const S_COMMENT_ENDING = "sCommentEnding"; // <!-- blah -
const S_COMMENT_ENDED = "sCommentEnded"; // <!-- blah --
const S_CDATA = "sCData"; // <![CDATA[ something
const S_CDATA_ENDING = "sCDataEnding"; // ]
const S_CDATA_ENDING_2 = "sCDataEnding2"; // ]]
const S_PI = "sPI"; // <?hi
const S_PI_BODY = "sPIBody"; // <?hi there
const S_PI_ENDING = "sPIEnding"; // <?hi "there" ?
const S_OPEN_TAG = "sOpenTag"; // <strong
const S_OPEN_TAG_SLASH = "sOpenTagSlash"; // <strong /
const S_ATTRIB = "sAttrib"; // <a
const S_ATTRIB_NAME = "sAttribName"; // <a foo
const S_ATTRIB_NAME_SAW_WHITE = "sAttribNameSawWhite"; // <a foo _
const S_ATTRIB_VALUE = "sAttribValue"; // <a foo=
const S_ATTRIB_VALUE_QUOTED = "sAttribValueQuoted"; // <a foo="bar
const S_ATTRIB_VALUE_CLOSED = "sAttribValueClosed"; // <a foo="bar"
const S_ATTRIB_VALUE_UNQUOTED = "sAttribValueUnquoted"; // <a foo=bar
const S_CLOSE_TAG = "sCloseTag"; // </a
const S_CLOSE_TAG_SAW_WHITE = "sCloseTagSawWhite"; // </a >

// These states are internal to sPIBody
const S_XML_DECL_NAME_START = 1; // <?xml
const S_XML_DECL_NAME = 2; // <?xml foo
const S_XML_DECL_EQ = 3; // <?xml foo=
const S_XML_DECL_VALUE_START = 4; // <?xml foo=
const S_XML_DECL_VALUE = 5; // <?xml foo="bar"

const SPACE_SEPARATOR = "SPACE_SEPARATOR";

Expand Down Expand Up @@ -454,7 +425,7 @@ class SaxesParser {
c: undefined,
};
while (chunkState.i < limit) {
const handler = this[STATE_TO_METHOD_NAME[this.state]];
const handler = this[this.state];
if (!handler) {
throw new Error(this, `Unknown state: ${this.state}`);
}
Expand Down

0 comments on commit 3869908

Please sign in to comment.