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: Binary search in token store utils.search #17066

Merged
merged 3 commits into from Apr 11, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/source-code/token-store/utils.js
Expand Up @@ -30,9 +30,20 @@ function getStartLocation(token) {
* @returns {number} The found index or `tokens.length`.
*/
exports.search = function search(tokens, location) {
const index = tokens.findIndex(el => location <= getStartLocation(el));
for (let minIndex = 0, maxIndex = tokens.length - 1; minIndex <= maxIndex;) {
const index = Math.trunc((minIndex + maxIndex) / 2);
fasttime marked this conversation as resolved.
Show resolved Hide resolved
const token = tokens[index];

return index === -1 ? tokens.length : index;
if (location <= getStartLocation(token)) {
if (index === minIndex) {
return index;
}
maxIndex = index;
} else {
minIndex = index + 1;
}
}
return tokens.length;
};

/**
Expand Down