-
Notifications
You must be signed in to change notification settings - Fork 34
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
Add rule preferring usage of Char over String if possible for single character Strings #90
Comments
Using a Char instead of String is not always better and it's impossible to detect automatically. |
@straight-shoota could you please share an example when it is not better? |
@straight-shoota I'm quite surprised but from a preliminary testing I just did it seems that in some cases using Benchmark of
|
Whoaaa, there's sth srsly wrong with some of the methods accepting require "benchmark"
N = 10_000
str = "#{"x" * 100}/#{"y" * 100}@#{"z" * 100}"
Benchmark.ips do |x|
x.report("String#includes?(Char)") do
N.times { str.includes?('@') }
end
x.report("String#includes?(String)") do
N.times { str.includes?("@") }
end
end
|
@Sija The difference of several magnitudes is very likely caused by an unintended performance optimization. Note that |
Seems you're right, removing
True that, although creating |
@Sija @straight-shoota what is your suggestion here? Should we force using char over string in some cases or not? |
Closing this for now, @Sija be free to re-open for further discussion. |
In many cases single character
String
s could be safely replaced byChar
s ("."
=>'.'
), e.g.:⚡️ Benchmark:
Several PRs regarding this, made by yours truly:
The text was updated successfully, but these errors were encountered: