Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add new Performance/AncestorsInclude cop #123

Merged
merged 1 commit into from
Jun 6, 2020

Conversation

fatkodima
Copy link
Contributor

Related link - https://github.com/JuanitoFatas/fast-ruby#ancestorsinclude-vs--code

Benchmark

# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
  gem 'benchmark-ips'
  gem 'benchmark-memory'
end

def fast
  (Class <= Class)
  (Class <= Module)
  (Class <= Object)
  (Class <= Kernel)
  (Class <= BasicObject)
end

def slow
  Class.ancestors.include?(Class)
  Class.ancestors.include?(Module)
  Class.ancestors.include?(Object)
  Class.ancestors.include?(Kernel)
  Class.ancestors.include?(BasicObject)
end

puts('********* IPS *********')

Benchmark.ips do |x|
  x.report('less than or equal') { fast }
  x.report('ancestors.include?') { slow }
  x.compare!
end

puts "********* MEMORY *********"

Benchmark.memory do |x|
  x.report('less than or equal') { fast }
  x.report('ancestors.include?') { slow }
  x.compare!
end

Results

********* IPS *********
Warming up --------------------------------------
  less than or equal   444.389k i/100ms
  ancestors.include?    67.737k i/100ms
Calculating -------------------------------------
  less than or equal      4.947M (± 5.9%) i/s -     24.886M in   5.049943s
  ancestors.include?    641.527k (±27.5%) i/s -      2.709M in   5.082782s

Comparison:
  less than or equal:  4947152.6 i/s
  ancestors.include?:   641526.9 i/s - 7.71x  (± 0.00) slower

********* MEMORY *********
Calculating -------------------------------------
  less than or equal     0.000  memsize (     0.000  retained)
                         0.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
  ancestors.include?     1.000k memsize (     0.000  retained)
                         5.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)

Comparison:
  less than or equal:          0 allocated
  ancestors.include?:       1000 allocated - Infx more

@fatkodima fatkodima force-pushed the subclass_check-cop branch 2 times, most recently from 2f11758 to 473c201 Compare June 2, 2020 19:58
config/default.yml Outdated Show resolved Hide resolved
@fatkodima fatkodima force-pushed the subclass_check-cop branch 2 times, most recently from 85c2212 to 33e12bc Compare June 3, 2020 12:21
@fatkodima
Copy link
Contributor Author

Updated.

@fatkodima fatkodima changed the title Add new Performance/SubclassCheck cop Add new Performance/AncestorsInclude cop Jun 3, 2020
CHANGELOG.md Show resolved Hide resolved
@fatkodima
Copy link
Contributor Author

@koic updated

CHANGELOG.md Outdated Show resolved Hide resolved
@koic koic merged commit fc4e1c3 into rubocop:master Jun 6, 2020
@koic
Copy link
Member

koic commented Jun 6, 2020

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants