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
Prefer grep
/grep_v
#8327
Comments
I have run a quick benchmark. Looks like Benchmark code# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
gem 'benchmark-ips'
gem 'benchmark-memory'
end
arr = ['foobar', 'foobaz', 'bazquux']
REGEXP = /foo/
def select_match(arr)
arr.select { |e| e.match?(REGEXP) }
end
def grep(arr)
arr.grep(REGEXP)
end
Benchmark.ips do |x|
x.report("select.match?") { select_match(arr) }
x.report("grep") { grep(arr) }
x.compare!
end
puts "********* MEMORY *********"
Benchmark.memory do |x|
x.report("select.match?") { select_match(arr) }
x.report("grep") { grep(arr) }
x.compare!
end
|
Kudos for checking that @fatkodima I would not have thought so. It makes some sense, because |
Please, do 👍 |
@marcandre |
Sure. What would be worth implementing is fixing 17030 :-) IIUC, Matz is ok with optimizing this |
The following code has 3 anti-patterns:
This issue is to identify that
enum.reject { |x| x.match? regexp }
should beenum.grep_v(regexp)
. Same withselect
vsgrep
.See: rubocop/rubocop-ast#65
The text was updated successfully, but these errors were encountered: