Skip to content

Instantly share code, notes, and snippets.

@marcandre
Created August 24, 2020 19:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
# 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