-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Improve Performance/StringReplacement
cop
#379
Comments
And the second case. https://github.com/shopify/ruby-style-guide#strings https://github.com/fastruby/fast-ruby/blob/main/code/string/gsub-vs-tr-vs-delete.rb (similar to this) Behaviorstr = 'lisp-case-rules'
# bad
str.gsub(/[aeiou]/, '')
# good
str.delete('aeiou') I've benchmarked it with require 'benchmark/ips'
URL = 'lisp-case-rules'
def slow
URL.gsub(/[aeiou]/, '')
end
def fast
URL.delete('aeiou')
end
Benchmark.ips do |x|
x.report('String#gsub') { slow }
x.report('String#delete') { fast }
x.compare!
end Ruby 3.3.0ydakuka@yauhenid:~/ruby-docker-app$ docker run ruby-app
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
Warming up --------------------------------------
String#gsub 48.726k i/100ms
String#delete 344.436k i/100ms
Calculating -------------------------------------
String#gsub 462.242k (± 2.7%) i/s - 2.339M in 5.063481s
String#delete 3.200M (± 2.7%) i/s - 16.188M in 5.062611s
Comparison:
String#delete: 3200076.3 i/s
String#gsub: 462242.4 i/s - 6.92x slower |
'aaa'.sub('a', 'x') # "xaa"
'aaa'.gsub('a', 'x') # "xxx" |
I know, that's why my examples are given for unambiguous cases. |
How rubocop can guess if you need to replace all occurrences or just one? |
I meant that the args passed to |
Description
Don’t use
String#gsub
in scenarios in which you can use a faster and more specialized alternative.https://rubystyle.guide/#dont-abuse-gsub
https://docs.rubocop.org/rubocop-performance/cops_performance.html#performancestringreplacement
https://github.com/fastruby/fast-ruby/blob/main/code/string/gsub-vs-sub.rb
Behavior
The text was updated successfully, but these errors were encountered: