Created
January 6, 2015 11:26
-
-
Save ccocchi/dbd8c254f9db5a93b277 to your computer and use it in GitHub Desktop.
presence? comes at a cost
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
require 'benchmark/ips' | |
require 'active_support/core_ext/object/blank' | |
find_result = nil | |
result = Benchmark.ips do |x| | |
x.report('==') do |times| | |
i = 0 | |
while i < times | |
1 if find_result == nil | |
i += 1 | |
end | |
end | |
x.report('!') do |times| | |
i = 0 | |
while i < times | |
1 if !find_result | |
i += 1 | |
end | |
end | |
x.report('nil?') do |times| | |
i = 0 | |
while i < times | |
1 if find_result.nil? | |
i += 1 | |
end | |
end | |
x.report('present?') do |times| | |
i = 0 | |
while i < times | |
1 if !find_result.present? | |
i += 1 | |
end | |
end | |
end | |
Benchmark.compare *result |
Author
ccocchi
commented
Jan 6, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment