From e3479a77812fca29bbf7cbc2e498cd74430adf81 Mon Sep 17 00:00:00 2001 From: Theo Date: Fri, 15 Apr 2016 16:06:18 +0200 Subject: [PATCH] Simplify and optimize Util.falsey? The two first cases (nil and false) are equivalent to !value, the three following (String, Hash, Array) are covered by #empty? and the last case (Struct) can use #any? These optimizations aren't huge in terms of operations per second, but they avoid allocating strings, arrays and hashes, which means that the GC will have to work less, leading to better overall performance. --- lib/jmespath/util.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/jmespath/util.rb b/lib/jmespath/util.rb index f5f87bf..9605393 100644 --- a/lib/jmespath/util.rb +++ b/lib/jmespath/util.rb @@ -8,15 +8,11 @@ class << self # https://github.com/jmespath/jmespath.site/blob/master/docs/proposals/improved-filters.rst#and-expressions-1 # def falsey?(value) - value.nil? || - value === false || - value == '' || - value == {} || - value == [] || - (value.respond_to?(:entries) && value.entries.compact.empty?) + !value || + (value.respond_to?(:empty?) && value.empty?) || + (value.respond_to?(:entries) && !value.entries.any?) # final case necessary to support Enumerable and Struct end - end end end