Skip to content

Commit

Permalink
perf(parser): optimize v8 perf deoptimizations (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiLiZART authored Apr 5, 2020
1 parent e3727dc commit 97ecba0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
69 changes: 60 additions & 9 deletions packages/bbob-parser/src/Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,70 @@ const TOKEN_TYPE_ATTR_VALUE = 'attr-value';
const TOKEN_TYPE_SPACE = 'space';
const TOKEN_TYPE_NEW_LINE = 'new-line';

const getTokenValue = (token) => token[TOKEN_VALUE_ID];
const getTokenLine = (token) => token[TOKEN_LINE_ID];
const getTokenColumn = (token) => token[TOKEN_COLUMN_ID];
/**
* @param {Token} token
* @returns {string}
*/
const getTokenValue = (token) => {
if (token && typeof token[TOKEN_VALUE_ID] !== 'undefined') {
return token[TOKEN_VALUE_ID];
}

return '';
};
/**
* @param {Token}token
* @returns {number}
*/
const getTokenLine = (token) => (token && token[TOKEN_LINE_ID]) || 0;
const getTokenColumn = (token) => (token && token[TOKEN_COLUMN_ID]) || 0;

/**
* @param {Token} token
* @returns {boolean}
*/
const isTextToken = (token) => {
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE
|| token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE
|| token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD;
}

const isTextToken = (token) => token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE
|| token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE
|| token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD;
return false;
};

/**
* @param {Token} token
* @returns {boolean}
*/
const isTagToken = (token) => {
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG;
}

const isTagToken = (token) => token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG;
return false;
};
const isTagEnd = (token) => getTokenValue(token).charCodeAt(0) === SLASH.charCodeAt(0);
const isTagStart = (token) => !isTagEnd(token);
const isAttrNameToken = (token) => token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME;
const isAttrValueToken = (token) => token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE;
const isAttrNameToken = (token) => {
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME;
}

return false;
};

/**
* @param {Token} token
* @returns {boolean}
*/
const isAttrValueToken = (token) => {
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE;
}

return false;
};

const getTagName = (token) => {
const value = getTokenValue(token);
Expand Down
12 changes: 10 additions & 2 deletions packages/bbob-parser/src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,23 @@ const parse = (input, opts = {}) => {
const getNodes = () => {
const lastNestedNode = nestedNodes.getLast();

return lastNestedNode ? lastNestedNode.content : nodes.toArray();
if (lastNestedNode && Array.isArray(lastNestedNode.content)) {
return lastNestedNode.content;
}

return nodes.toArray();
};

/**
* @private
* @param {TagNode} tag
*/
const appendNodes = (tag) => {
getNodes().push(tag);
const items = getNodes();

if (Array.isArray(items)) {
items.push(tag);
}
};

/**
Expand Down
26 changes: 23 additions & 3 deletions packages/bbob-parser/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,22 @@ export const createCharGrabber = (source, options) => {

return source.substr(start, cursor.pos - start);
},
getNext: () => source[cursor.pos + 1],
getPrev: () => source[cursor.pos - 1],
getNext: () => {
const nextPos = cursor.pos + 1;

if (nextPos <= (source.length - 1)) {
return source[nextPos];
}
return null;
},
getPrev: () => {
const prevPos = cursor.pos - 1;

if (typeof source[prevPos] !== 'undefined') {
return source[prevPos];
}
return null;
},
getCurr,
getRest,
/**
Expand Down Expand Up @@ -127,7 +141,13 @@ export const createList = (values = []) => {
/**
* @callback getLastCb
*/
const getLast = () => (nodes.length ? nodes[nodes.length - 1] : null);
const getLast = () => {
if (Array.isArray(nodes) && nodes.length > 0 && typeof nodes[nodes.length - 1] !== 'undefined') {
return nodes[nodes.length - 1];
}

return null;
};
/**
* @callback flushLastCb
* @return {*}
Expand Down

0 comments on commit 97ecba0

Please sign in to comment.