Skip to content
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

Suggest compact over reject nil values #8566

Closed
baelter opened this issue Aug 19, 2020 · 1 comment · Fixed by #8814
Closed

Suggest compact over reject nil values #8566

baelter opened this issue Aug 19, 2020 · 1 comment · Fixed by #8814

Comments

@baelter
Copy link

baelter commented Aug 19, 2020

Rubocop could suggest replacing:

{}.reject { |_k, v| v.nil? } with {}.compact

@rrosenblum
Copy link
Contributor

rrosenblum commented Aug 19, 2020

It looks like this would be a good candidate for a performance cop. I haven't tested this with a hash yet. I would expect to see similar results from a hash.

ARRAY_100_NIL = Array.new(100)
ARRAY_1000_NIL = Array.new(1000)
ARRAY_100 = (1..100).to_a.map { |v| rand(3) >= 2 ? nil : v }
ARRAY_1000 = (1..1000).to_a.map { |v| rand(3) >= 2 ? nil : v }

Benchmark.ips do |x|
  x.report('100 nil reject') { ARRAY_100_NIL.reject(&:nil?) }
  x.report('1000 nil reject') { ARRAY_1000_NIL.reject(&:nil?) }
  x.report('100 reject') { ARRAY_100.reject(&:nil?) }
  x.report('1000 reject') { ARRAY_1000.reject(&:nil?) }
  x.report('100 nil compact') { ARRAY_100_NIL.compact }
  x.report('1000 nil compact') { ARRAY_1000_NIL.compact }
  x.report('100 compact') { ARRAY_100.compact }
  x.report('1000 compact') { ARRAY_1000.compact }
  x.compare!
end

Warming up --------------------------------------
      100 nil reject    30.078k i/100ms
     1000 nil reject     3.094k i/100ms
          100 reject    18.859k i/100ms
         1000 reject     2.010k i/100ms
     100 nil compact   317.667k i/100ms
    1000 nil compact   123.605k i/100ms
         100 compact   146.194k i/100ms
        1000 compact    29.429k i/100ms
Calculating -------------------------------------
      100 nil reject    297.746k (± 3.7%) i/s -      1.504M in   5.058602s
     1000 nil reject     31.080k (± 2.4%) i/s -    157.794k in   5.080001s
          100 reject    194.301k (± 4.3%) i/s -    980.668k in   5.056359s
         1000 reject     22.523k (± 3.8%) i/s -    112.560k in   5.004890s
     100 nil compact      3.336M (± 3.9%) i/s -     16.836M in   5.054570s
    1000 nil compact      1.260M (± 2.7%) i/s -      6.304M in   5.007786s
         100 compact      1.743M (±14.5%) i/s -      8.625M in   5.057635s
        1000 compact    345.905k (±24.5%) i/s -      1.648M in   5.047389s

Comparison:
     100 nil compact:  3336043.7 i/s
         100 compact:  1743292.6 i/s - 1.91x  (± 0.00) slower
    1000 nil compact:  1259767.4 i/s - 2.65x  (± 0.00) slower
        1000 compact:   345904.8 i/s - 9.64x  (± 0.00) slower
      100 nil reject:   297745.5 i/s - 11.20x  (± 0.00) slower
          100 reject:   194301.3 i/s - 17.17x  (± 0.00) slower
     1000 nil reject:    31079.8 i/s - 107.34x  (± 0.00) slower
         1000 reject:    22523.0 i/s - 148.12x  (± 0.00) slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants