Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(parser): cache nested tokens in Set to prevent deoptimization #83

Merged
merged 3 commits into from
Dec 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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