#1342·acorn

Replace keyword Regexps with Sets for improved performance

Author: dmichon-msftCreated Jan 23, 2025Updated May 12, 2025

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:

javascript
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)
}

Existing implementation: Image

Impact of suggested implementation: Image