Skip to content

[prefer-regexp-exec] Justification linked to on Stack Overflow is missing (and contradicted) #3389

@rrthomas

Description

@rrthomas

[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

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationDocumentation ("docs") that needs adding/updatingduplicateThis issue or pull request already existspackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions