rails/rails — pull request #17182 by vais (2014-10-05 08:05:20+00:00)
Title: Faster String#blank? (140% to 470% improvement)

Benchmarking code:
```ruby
require 'benchmark/ips'
require 'active_support/all'

class String
def improved_blank?
return empty? || strip.empty? if ascii_only?
BLANK_RE === self
end
end

puts "EMPTY STRING"
Benchmark.ips do |x|
x.report('original') { ''.blank? }
x.report('improved') { ''.improved_blank? }
x.compare!
end

puts "ASCII STRING"
Benchmark.ips do |x|
x.report('original') { ' abcd '.blank? }
x.report('improved') { ' abcd '.improved_blank? }
x.compare!
end

puts "UNICODE STRING"
Benchmark.ips do |x|
x.report('original') { "\u00a0".blank? }
x.report('improved') { "\u00a0".improved_blank? }
x.compare!
end
```

Results:
```
EMPTY STRING
Calculating -------------------------------------
original 9173 i/100ms
improved 15256 i/100ms
-------------------------------------------------
original 352351.5 (±18.4%) i/s - 1706178 in 5.011738s
improved 1658752.5 (±21.4%) i/s - 7795816 in 4.998368s

Comparison:
improved: 1658752.5 i/s
original: 352351.5 i/s - 4.71x slower

ASCII STRING
Calculating -------------------------------------
original 11325 i/100ms
improved 13602 i/100ms
-------------------------------------------------
original 671560.4 (±16.3%) i/s - 3261600 in 5.001736s
improved 948691.9 (±20.0%) i/s - 4543068 in 4.994523s

Comparison:
improved: 948691.9 i/s
original: 671560.4 i/s - 1.41x slower

UNICODE STRING
Calculating -------------------------------------
original 9198 i/100ms
improved 9023 i/100ms
-------------------------------------------------
original 331457.0 (±18.0%) i/s - 1609650 in 5.013549s
improved 326054.9 (±17.3%) i/s - 1588048 in 5.020778s

Comparison:
original: 331457.0 i/s
improved: 326054.9 i/s - 1.02x slower
```