Skip to content

Instantly share code, notes, and snippets.

@marcandre
Created August 24, 2020 19:37
Show Gist options
  • Save marcandre/8642bcfcc5fea7f0ac7779e415401535 to your computer and use it in GitHub Desktop.
Save marcandre/8642bcfcc5fea7f0ac7779e415401535 to your computer and use it in GitHub Desktop.
# gem install 'benchmark-ips'
require 'benchmark/ips'
long = ' ' * 1_000_000 + 'x'
REGEXP = /\A[[:space:]]*\z/
[' ', 'hello', long].each do |str|
Benchmark.ips do |x|
x.report('with_regexp') { REGEXP.match? str }
x.report('with_lstrip') { str.lstrip.empty? }
x.compare!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment