Project

General

Profile

Actions

Patch #37591

closed

Use start_with? or end_with? to check the first or last character of a string

Added by Go MAEDA about 2 years ago. Updated about 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Code cleanup/refactoring
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

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

use_start_with.patch (2.38 KB) use_start_with.patch Go MAEDA, 2022-08-22 11:19
Actions

Also available in: Atom PDF