Created
April 12, 2012 16:24
-
-
Save ch1ago/2368818 to your computer and use it in GitHub Desktop.
current_user performance :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hey guys, since current_user is a method used in every request I decided to give it a few benchmarks | |
I'm gisting this so if anyone may find good literature | |
here was my start point question: | |
which is best? | |
(yes this approach doesn't validate an auth token) | |
def current_user | |
if session[:user_id] | |
@current_user ||= User.find(session[:user_id]) | |
end | |
@current_user | |
end | |
def current_user | |
@current_user ||= User.find(session[:user_id]) rescue nil | |
end | |
benchmarks: | |
require 'benchmark' | |
TIMES = 100 | |
Benchmark.bmbm do |x| | |
x.report("where first number") do | |
TIMES.times { |i| User.where(:id=>i+100).first rescue nil } | |
end | |
x.report("where first nil") do | |
TIMES.times { |i| User.where(:id=>nil).first rescue nil } | |
end | |
x.report("exception number") do | |
TIMES.times { |i| User.find(i+100) rescue nil} | |
end | |
x.report("exception nil") do | |
TIMES.times { User.find(nil) rescue nil } | |
end | |
x.report("if nil") do | |
TIMES.times { User.find(nil) rescue nil if nil } | |
end | |
end | |
user system total real | |
where first number 0.070000 0.000000 0.070000 ( 0.084907) | |
where first nil 0.080000 0.000000 0.080000 ( 0.083039) | |
exception number 0.070000 0.000000 0.070000 ( 0.076446) | |
exception nil 0.010000 0.000000 0.010000 ( 0.007718) | |
if nil 0.010000 0.000000 0.010000 ( 0.000077) | |
where first number | |
User Load (2.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 100 LIMIT 1 | |
where first nil | |
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1 | |
exception number | |
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 100]] | |
TIMES = 100_000 | |
Benchmark.bmbm do |x| | |
x.report("exception nil") do | |
TIMES.times { User.find(nil) rescue nil } | |
end | |
x.report("if nil") do | |
TIMES.times { User.find(nil) rescue nil if nil } | |
end | |
x.report("if nil present?") do | |
TIMES.times { User.find(nil) rescue nil if nil.present? } | |
end | |
x.report("if nil nil?") do | |
TIMES.times { User.find(nil) rescue nil if nil.present? } | |
end | |
x.report("unless nil nil?") do | |
TIMES.times { User.find(nil) rescue nil unless nil.nil? } | |
end | |
x.report("unless nil blank?") do | |
TIMES.times { User.find(nil) rescue nil unless nil.blank? } | |
end | |
end | |
user system total real | |
exception nil 11.070000 0.070000 11.140000 ( 11.160613) | |
if nil 0.020000 0.000000 0.020000 ( 0.014595) | |
if nil present? 0.040000 0.000000 0.040000 ( 0.042207) | |
if nil nil? 0.040000 0.000000 0.040000 ( 0.041937) | |
unless nil nil? 0.030000 0.000000 0.030000 ( 0.024599) | |
unless nil blank? 0.030000 0.000000 0.030000 ( 0.027269) | |
if not nil nil? 0.030000 0.000000 0.030000 ( 0.027878) | |
if not nil blank? 0.030000 0.000000 0.030000 ( 0.028738) | |
if ! nil nil? 0.030000 0.000000 0.030000 ( 0.027755) | |
if ! nil blank? 0.030000 0.000000 0.030000 ( 0.028921) | |
conclusion: | |
1. where :id sucks | |
2. find(nil) runs faster than find(number) | |
3. unless, if, if not, if ! => are the same | |
4. nil.checkmethod? looks nicer but runs slower | |
5. if value => most fastest | |
so this method is the ultimate bestest | |
def current_user | |
@current_user ||= User.find(session[:user_id]) rescue nil if session[:user_id] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment