Use #== in Conversion.boolean?

On JRuby 9.0.1.0 this is a bit faster than using "is_a?":

    require 'benchmark/ips'

    input = false

    Benchmark.ips do |bench|
      bench.report 'is_a?' do
        input.is_a?(TrueClass) || input.is_a?(FalseClass)
      end

      bench.report '==' do
        input == true || input == false
      end

      bench.compare!
    end

This outputs:

    Calculating -------------------------------------
                   is_a?    86.129k i/100ms
                      ==   112.837k i/100ms
    -------------------------------------------------
                   is_a?      7.375M (±15.3%) i/s -     35.227M
                      ==     10.428M (±12.0%) i/s -     50.889M

    Comparison:
                      ==: 10427617.5 i/s
                   is_a?:  7374666.2 i/s - 1.41x slower

On both MRI 2.2 and Rubinius 2.5.8 there's little to no difference
between these two methods.
This commit is contained in:
Yorick Peterse 2015-09-23 16:35:09 +02:00
parent d815437217
commit 9bb908f8b1
1 changed files with 1 additions and 1 deletions

View File

@ -89,7 +89,7 @@ module Oga
# @return [TrueClass|FalseClass]
def self.boolean?(value)
value.is_a?(TrueClass) || value.is_a?(FalseClass)
value == true || value == false
end
# @param [Oga::XML::NodeSet] set