跳转到内容

useTopLevelRegex (since v1.8.0)

Diagnostic Category: lint/nursery/useTopLevelRegex

Require regex literals to be declared at the top level.

This rule is useful to avoid performance issues when using regex literals inside functions called many times (hot paths). Regex literals create a new RegExp object when they are evaluated. (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) By declaring them at the top level, this overhead can be avoided.

It’s important to note that this rule is not recommended for all cases. Placing regex literals at the top level can hurt startup times. In browser contexts, this can result in longer page loads.

Additionally, this rule ignores regular expressions with the g and/or y flags, as they maintain internal state and can cause side effects when calling test and exec with them.

function foo(someString) {
return /[a-Z]*/.test(someString)
}
code-block.js:2:12 lint/nursery/useTopLevelRegex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   This regex literal is not defined in the top level scope. This can lead to performance issues if this function is called frequently.
  
    1 │ function foo(someString) {
  > 2 │     return /[a-Z]*/.test(someString)
              ^^^^^^^^
    3 │ }
    4 │ 
  
   Move the regex literal outside of this scope, and place it at the top level of this module, as a constant.
  
const REGEX = /[a-Z]*/;
function foo(someString) {
return REGEX.test(someString)
}
function foo(str) {
return /[a-Z]*/g.exec(str)
}