Actions
Patch #37591
closedUse start_with? or end_with? to check the first or last character of a string
Description
String#start_with?
and String#end_with?
are more intuitive and faster than [0, 1]
or [-1, 1]
.
require 'benchmark/ips'
Benchmark.ips do |bench|
path = '/foo'
bench.report('[0, 1]') do
path[0, 1] == '/'
end
bench.report('start_with?') do
path.start_with?('/')
end
bench.compare!
end
Warming up -------------------------------------- [0, 1] 514.843k i/100ms start_with? 830.117k i/100ms Calculating ------------------------------------- [0, 1] 5.201M (± 3.1%) i/s - 26.257M in 5.053268s start_with? 8.254M (± 3.2%) i/s - 41.506M in 5.034294s Comparison: start_with?: 8253500.9 i/s [0, 1]: 5201251.4 i/s - 1.59x (± 0.00) slower
Files
Actions