Skip to content

Instantly share code, notes, and snippets.

@zismailov
Created October 1, 2021 06:52
Show Gist options
  • Save zismailov/ccbdd7daf7fb1e3113fc8202401b2417 to your computer and use it in GitHub Desktop.
Save zismailov/ccbdd7daf7fb1e3113fc8202401b2417 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
Benchmark.ips do |x|
x.report('#exists?') { User.where(id: 1).exists? }
x.report('#any?') { User.where(id: 1).any? }
x.report('#present?') { User.where(id: 1).present? }
x.compare!
end
# Warming up --------------------------------------
# #exists? 114.000 i/100ms
# #any? 148.000 i/100ms
# #present? 150.000 i/100ms
# Calculating -------------------------------------
# #exists? 1.839k (± 3.2%) i/s - 9.234k in 5.026604s
# #any? 1.296k (±32.5%) i/s - 6.068k in 5.130723s
# #present? 1.595k (± 3.6%) i/s - 8.100k in 5.085038s
#
# Comparison:
# #exists?: 1839.0 i/s
# #present?: 1595.1 i/s - 1.15x slower
# #any?: 1296.5 i/s - 1.42x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment