-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
[This is a documentation issue, not a code issue, so I've omitted irrelevant parts of the template; however, I have read them all!]
The documentation for this rule quotes a Stack Overflow page in support of its contention that RegExp#exec
is faster than String#match
if used without a global flag, and quotes the following text:
RegExp.prototype.exec is a lot faster than String.prototype.match, but that’s because they are not exactly the same thing, they are different.
I went to the given link, but could not find that text. Indeed, one answer to the question actually makes the reverse assertion:
It is worth to mention that if the outcome of string.match() and regex.exec() are the same (ex: when not using \g flag), regex.exec() will take somewhere between x2 to x30 then string.match():
I must admit, I don't understand either assertion, in the light of the quote from the MDN page, which is still there:
If the regular expression does not include the g flag, returns the same result as RegExp.exec().
Given this, it would seem that having determined that the g
flag is not used, any sane implementation would simply use its RegExp#exec
to implement String#match
; in other words, I'd expect them to be the same speed.
I did some simple timings with Node 14, and in fact the assertion does appear to be right in my simple test case with Node on my machine: Regex#exec
comes out faster than String#match
; but it would be good to have a stronger justification than a quote from a SO question that is no longer visible on the page, and preferably an explanation!
Versions
package | version |
---|---|
@typescript-eslint/eslint-plugin |
4.22.0 |