You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Fix#482] Change Performance/CollectionLiteralInLoop to not register offenses for Array#include? that are optimized directly in Ruby.
Since `include?` on arrays are the only instances I ever run across this cop, I think it makes sense to add special handling for this.
On Ruby 3.4 this does no array allocations:
```
require "memory_profiler"
class Foo
def bar
Bar.new
end
end
class Bar
def baz
end
end
foo = Foo.new
report = MemoryProfiler.report do
[1,2].include?(foo.bar.baz)
end.pretty_print
```
Also, this code is simply slower on Ruby 3.4:
```rb
require "benchmark/ips"
local = [301, 302]
val = 301
Benchmark.ips do |x|
x.report('local') do
local.include?(val)
end
x.report('literal') do
[301, 302].include?(val)
end
x.compare!
end
```
```
$ ruby test.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
local 1.822M i/100ms
literal 2.296M i/100ms
Calculating -------------------------------------
local 18.235M (± 1.6%) i/s (54.84 ns/i) - 92.906M in 5.096277s
literal 22.807M (± 1.5%) i/s (43.85 ns/i) - 114.789M in 5.034289s
Comparison:
literal: 22806932.0 i/s
local: 18235340.7 i/s - 1.25x slower
```
*[#482](https://github.com/rubocop/rubocop-performance/issues/482): Change `Performance/CollectionLiteralInLoop` to not register offenses for `Array#include?` that are optimized directly in Ruby. ([@earlopain][])
0 commit comments