Add `String#match?` to `String#start_with?`/`String#end_with?` compar… · fastruby/fast-ruby@e951321 · GitHub
Skip to content

Commit e951321

Browse files
committed
Add String#match? to String#start_with?/String#end_with? comparison
1 parent 9907db5 commit e951321

3 files changed

Lines changed: 42 additions & 30 deletions

File tree

‎README.md‎

Lines changed: 26 additions & 24 deletions
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
require 'benchmark/ips'
22

3-
SLUG = 'root_url'
3+
SLUG = "some_kind_of_root_url"
44

5-
def slow
5+
def slower
66
SLUG =~ /_(path|url)$/
77
end
88

9+
def slow
10+
SLUG.match?(/_(path|url)$/)
11+
end
12+
913
def fast
1014
SLUG.end_with?('_path', '_url')
1115
end
1216

1317
Benchmark.ips do |x|
14-
x.report('String#=~') { slow }
18+
x.report('String#=~') { slower }
19+
x.report('String#match?') { slow } if RUBY_VERSION >= "2.4.0".freeze
1520
x.report('String#end_with?') { fast }
1621
x.compare!
1722
end
Lines changed: 8 additions & 3 deletions

0 commit comments

Comments
 (0)