tshet-uinh-examples icon indicating copy to clipboard operation
tshet-uinh-examples copied to clipboard

Avoid `String#match`

Open graphemecluster opened this issue 10 months ago • 3 comments

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

graphemecluster avatar Jan 15 '25 21:01 graphemecluster

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.

syimyuzya avatar Jan 17 '25 06:01 syimyuzya

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

graphemecluster avatar Jan 24 '25 17:01 graphemecluster

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

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

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

syimyuzya avatar Jan 26 '25 07:01 syimyuzya