Created
February 23, 2023 02:23
-
-
Save postmodern/66dfb41c8cc98f3bc3c2fd2fe7385542 to your computer and use it in GitHub Desktop.
Micro-benchmark for `value != nil` vs. `!value.nil?`
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
Benchmark.bm do |b| | |
n = 1_000_000 | |
value1 = 1 | |
value2 = nil | |
b.report('value != nil') do | |
n.times do | |
value1 != nil | |
value2 != nil | |
end | |
end | |
b.report('!value.nil?') do | |
n.times do | |
!value1.nil? | |
!value2.nil? | |
end | |
end | |
end |
@andreavocado it actually is! At first I didn't believe it would have improved the code, which is why I wrote the benchmark.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should be a rubocop cop, shouldn't it?