https://github.com/jekyll/jekyll
Raw File
Tip revision: 053acd31ac69f915d2e5035dac8a6c6e71ad413c authored by olivia on 25 January 2018, 18:34:49 UTC
Release :gem: 3.7.2
Tip revision: 053acd3
end-with-vs-regexp
#!/usr/bin/env ruby
require 'benchmark/ips'

Benchmark.ips do |x|
  path_without_ending_slash = '/some/very/very/long/path/to/a/file/i/like'
  x.report('no slash regexp')    { path_without_ending_slash =~ /\/$/ }
  x.report('no slash end_with?') { path_without_ending_slash.end_with?("/")  }
  x.report('no slash [-1, 1]') { path_without_ending_slash[-1, 1] == "/"  }
end

Benchmark.ips do |x|
  path_with_ending_slash = '/some/very/very/long/path/to/a/file/i/like/'
  x.report('slash regexp')    { path_with_ending_slash =~ /\/$/ }
  x.report('slash end_with?') { path_with_ending_slash.end_with?("/")  }
  x.report('slash [-1, 1]')   { path_with_ending_slash[-1, 1] == "/"  }
end
back to top