String#blank?
Ruby Extension
fast_blank
is a simple C extension which provides a fast implementation of Active Support's String#blank?
method.
How do you use it?
require 'fast_blank'
or add it to your Bundler Gemfile
gem 'fast_blank'
How fast is "Fast"?
About 1.2–20x faster than Active Support on my machine (your mileage my vary, depends on string length):
$ bundle exec ./benchmark
================== Test String Length: 0 ==================
Calculating -------------------------------------
Fast Blank 225.251k i/100ms
Fast ActiveSupport 225.676k i/100ms
Slow Blank 110.934k i/100ms
New Slow Blank 221.792k i/100ms
-------------------------------------------------
Fast Blank 29.673M (
Additionally, this gem allocates no strings during the test, making it less of a GC burden.
Compatibility note:
fast_blank
supports MRI Ruby 1.9.3, 2.0, 2.1, and 2.2, as well as Rubinius 2.x. Earlier versions of MRI are untested.
fast_blank
implements String#blank?
as MRI would have implemented it, meaning it has 100% parity with String#strip.length == 0
.
Active Support's version also considers Unicode spaces. For example, "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000".blank?
is true in Active Support even though fast_blank
would treat it as not blank. Therefore, fast_blank
also provides blank_as?
which is a 100%-compatible Active Support blank?
replacement.
Credits
- Author: Sam Saffron ([email protected])
- https://github.com/SamSaffron/fast_blank
- License: MIT
- Gem template based on CodeMonkeySteve/fast_xor
Change log:
1.0.1:
- Minor, avoid warnings if redefining blank?
1.0.0:
0.0.2: