Skip to content

Commit

Permalink
perf(parser): cache nested tokens in Set to prevent deoptimization (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiLiZART authored Dec 16, 2020
1 parent 70ff2e6 commit cad0e9e
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/bbob-parser/src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,31 @@ const parse = (input, opts = {}) => {

/**
* Cache for nested tags checks
* @type {{}}
*/
const nestedTagsMap = {};
const nestedTagsMap = new Set();

/**
*
* @param token
* @returns {boolean}
*/
const isTokenNested = (token) => {
if (typeof nestedTagsMap[token.getValue()] === 'undefined') {
nestedTagsMap[token.getValue()] = tokenizer.isTokenNested(token);
const value = token.getValue();

if (!nestedTagsMap.has(value) && tokenizer.isTokenNested && tokenizer.isTokenNested(token)) {
nestedTagsMap.add(value);

return true;
}

return nestedTagsMap[token.getValue()];
return nestedTagsMap.has(value);
};

/**
* @param tagName
* @returns {boolean}
*/
const isTagNested = (tagName) => !!nestedTagsMap[tagName];
const isTagNested = (tagName) => Boolean(nestedTagsMap.has(tagName));

/**
* @private
Expand Down

0 comments on commit cad0e9e

Please sign in to comment.