Skip to content
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

Avoid String#match #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Avoid String#match #82

wants to merge 1 commit into from

Conversation

graphemecluster
Copy link
Member

RegExp#test should be used when only a boolean is needed

Copy link
Member Author

@graphemecluster graphemecluster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is such a rule in Unicorn: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-regexp-test.md
I once wrote #70 (comment), but perhaps I should change my mind and include rules from Unicorn.

@syimyuzya
Copy link
Member

syimyuzya commented Jan 17, 2025

There is such a rule in Unicorn: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-regexp-test.md I once wrote #70 (comment), but perhaps I should change my mind and include rules from Unicorn.

It doesn't make sense to me.

The doc says "as it exclusively returns a boolean and therefore is more efficient", but modern JS engines just might not work that way. Even if it does save time or memory, I doubt if it would make any effect enough to be visible (other than a more inconsistent word order), given that every schema makes extensive use of 音韻地位.屬於, which I believe takes a lot more time than all other operations combined.

@graphemecluster
Copy link
Member Author

@syimyuzya 那個文檔是後加的,我重構前並不知道有這條規則。
而我也覺得問題並不在於執行速度。我覺得是在於各個方法應該有自己的分工,String#match 本來在意義層面上就不是用來判斷一個字串是否符合一個正則表達式的方法,它根本應該叫 allSubstringsMatchedBy

@syimyuzya
Copy link
Member

@syimyuzya 那個文檔是後加的,我重構前並不知道有這條規則。 而我也覺得問題並不在於執行速度。我覺得是在於各個方法應該有自己的分工,String#match 本來在意義層面上就不是用來判斷一個字串是否符合一個正則表達式的方法,它根本應該叫 allSubstringsMatchedBy

哦哦~我才反應過來 JS 的 String#match 作用和 RegExp.exec.test 甚至和 String#matchAll 差別都很大😂(之前我把它當成 RegExp#exec 類似的東西了🙈

那這個 PR 提到的幾處 .match 確實都換掉比較好~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants