From 9bb908f8b1f6c72582ae6070d30f8bd8316ec5ad Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 23 Sep 2015 16:35:09 +0200 Subject: [PATCH] Use #== in Conversion.boolean? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- lib/oga/xpath/conversion.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oga/xpath/conversion.rb b/lib/oga/xpath/conversion.rb index 30bd938..e9b6b27 100644 --- a/lib/oga/xpath/conversion.rb +++ b/lib/oga/xpath/conversion.rb @@ -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