Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rrthomas opened this issue May 15, 2021 · 3 comments · Fixed by #3392
Closed
Labels
documentation Documentation ("docs") that needs adding/updating duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@rrthomas
Copy link
Contributor

[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
@rrthomas rrthomas added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 15, 2021
@bradzacher
Copy link
Member

See duplicate issue #2280
If you want to update the rule docs - feel free!

@bradzacher bradzacher added documentation Documentation ("docs") that needs adding/updating duplicate This issue or pull request already exists and removed triage Waiting for team members to take a look labels May 15, 2021
@rrthomas
Copy link
Contributor Author

Thanks @bradzacher, done. I have rewritten the docs to give consistency as the primary justification, and the slight performance difference as the reason for choosing Regex#exec as the preferred form.

@mohd-akram
Copy link

This rule should definitely be removed from recommended, or entirely.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Documentation ("docs") that needs adding/updating duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants