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

Cop idea: Enumerable#each + push vs Enumerable#map #397

Open
ydakuka opened this issue Nov 26, 2023 · 5 comments
Open

Cop idea: Enumerable#each + push vs Enumerable#map #397

ydakuka opened this issue Nov 26, 2023 · 5 comments

Comments

@ydakuka
Copy link
Contributor

ydakuka commented Nov 26, 2023

https://github.com/fastruby/fast-ruby#enumerableeach--push-vs-enumerablemap-code

Describe the solution you'd like

class Test
  ARRAY = (1..100).to_a

  # bad
  def slow
    array = []
    ARRAY.each { |i| array.push i }
    array
  end

  # good
  def fast
    ARRAY.map { |i| i }
  end
end
@ydakuka
Copy link
Contributor Author

ydakuka commented Nov 26, 2023

I've benchmarked it (ruby 3.3.0).

ydakuka@yauhenid:~/ruby-docker-app$ docker run ruby-app  
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
Warming up --------------------------------------
                slow    12.748k i/100ms
                fast    20.212k i/100ms
Calculating -------------------------------------
                slow    135.016k (± 1.1%) i/s -    675.644k in   5.004757s
                fast    203.381k (± 0.9%) i/s -      1.031M in   5.068794s

Comparison:
                fast:   203381.5 i/s
                slow:   135016.1 i/s - 1.51x  slower

@koic
Copy link
Member

koic commented Nov 27, 2023

Due to the case of #384, can you verify the results with the latest Ruby 3.3.0dev? It might be a good idea for fastruby to redo benchmark with the latest Ruby version. It might be better to wait until after the scheduled release of Ruby 3.3 next month, rather than doing it now.

@ydakuka
Copy link
Contributor Author

ydakuka commented Nov 27, 2023

It might be better to wait until after the scheduled release of Ruby 3.3 next month, rather than doing it now.

I agree. After the release I'll do the benchmarks.

@ydakuka
Copy link
Contributor Author

ydakuka commented Nov 28, 2023

Related: rubocop/rubocop#11878

@ydakuka
Copy link
Contributor Author

ydakuka commented Jan 12, 2024

After the release I'll do the benchmarks.

I've updated benchmarks in my repos.

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

No branches or pull requests

2 participants