Patch #37591

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

Added by Go MAEDA about 1 month ago. Updated about 1 month ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Go MAEDA% Done:

0%

Category:Code cleanup/refactoring
Target version:5.1.0

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

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

Associated revisions

Revision 21774
Added by Go MAEDA about 1 month ago

Use start_with? or end_with? to check the first or last character of a string (#37591).

Patch by Go MAEDA.

History

#1 Updated by Go MAEDA about 1 month ago

  • Target version set to 5.1.0

Setting the target version to 5.1.0.

#2 Updated by Go MAEDA about 1 month ago

  • Status changed from New to Closed
  • Assignee set to Go MAEDA

Committed the patch.

Also available in: Atom PDF