-
Notifications
You must be signed in to change notification settings - Fork 944
Open
Description
On modern runtimes, testing identifiers for matches with keywords is significantly (up to 60%) faster by using Set than by using a RegExp.
The lookup is faster still if you first check the length of the identifier to avoid computing hashcodes for strings that are too long or too short to match.
Sample code:
function createTester(words) {
let minLength = Infinity;
let maxLength = 0;
const split = words.split(' ');
const set = new Set();
for (let i = 0; i < split.length; i++) {
const word = split[i];
set.add(word);
if (word.length < minLength) minLength = word.length;
if (word.length > maxLength) maxLength = word.length;
}
return regexpCache[words] = {
test: function (input) {
// Length testing the string is cheaper than computing the hash code for long strings
return input.length >= minLength && input.length <= maxLength && set.has(input)
}
}
}
export function wordsRegexp(words) {
return regexpCache[words] || createTester(words)
}
alexander-akait, Kapelianovych and GulgDev
Metadata
Metadata
Assignees
Labels
No labels