Skip to content

Instantly share code, notes, and snippets.

@bartoszkopinski
Last active May 8, 2016 23:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartoszkopinski/8049510 to your computer and use it in GitHub Desktop.
Save bartoszkopinski/8049510 to your computer and use it in GitHub Desktop.
Mongoid benchmark, #find vs. #where, #only vs. no #only
n = 1_000
id = User.first.id
profile = User.only(:profile).find(id).profile
Benchmark.bm(50) do |x|
x.report('User.find(id).profile') do
n.times{ User.find(id).profile }
end
x.report('User.find_by(id: id).profile') do
n.times{ User.find_by(id: id).profile }
end
x.report('User.where(id: id).first.profile') do
n.times{ User.where(id: id).first.profile }
end
x.report('User.only(:profile).find(id).profile') do
n.times{ User.only(:profile).find(id).profile }
end
x.report('User.where(id: id).only(:profile).first.profile') do
n.times{ User.where(id: id).only(:profile).first.profile }
end
end
user system total real
User.find(id).profile 1.230000 0.100000 1.330000 ( 1.524522)
User.find_by(id: id).profile 1.210000 0.090000 1.300000 ( 1.486113)
User.where(id: id).first.profile 1.250000 0.090000 1.340000 ( 1.537307)
User.only(:profile).find(id).profile 1.280000 0.100000 1.380000 ( 1.608061)
User.where(id: id).only(:profile).first.profile 1.190000 0.090000 1.280000 ( 1.476007)
[Finished in 14.8s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment