/
regexp-match.rb
87 lines (71 loc) · 1.45 KB
/
regexp-match.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true
# rubocop-performance
# Performance/RegexpMatch
# see https://github.com/rubocop-hq/rubocop-performance/blob/master/manual/cops_performance.md#performanceregexpmatch
require 'benchmark_driver'
output = :gruff
versions = ['2.4.6', '2.5.4', '2.6.3', '2.7.0-preview1']
Benchmark.driver(output: output) do |x|
x.rbenv *versions
x.prelude <<~RUBY
def do_something(arg = nil)
1 + 1
end
X = 'regex-match'
RE = /re/
def bad_sample1
if X =~ RE
do_something
end
end
def bad_sample2
if X !~ RE
do_something
end
end
def bad_sample3
if X.match(RE)
do_something
end
end
def bad_sample4
if RE === X
do_something
end
end
def good_sample1
if X.match?(RE)
do_something
end
end
def good_sample2
if !X.match?(RE)
do_something
end
end
def good_sample3
if X =~ RE
do_something(Regexp.last_match)
end
end
def good_sample4
if X.match(RE)
do_something($~)
end
end
def good_sample5
if RE === X
do_something($~)
end
end
RUBY
x.report %{ bad_sample1 }
x.report %{ bad_sample2 }
x.report %{ bad_sample3 }
x.report %{ bad_sample4 }
x.report %{ good_sample1 }
x.report %{ good_sample2 }
x.report %{ good_sample3 }
x.report %{ good_sample4 }
x.report %{ good_sample5 }
end