/
case-when-splat.rb
66 lines (56 loc) · 1.09 KB
/
case-when-splat.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
# frozen_string_literal: true
# rubocop-performance
# Performance/CaseWhenSplat
# see https://github.com/rubocop-hq/rubocop-performance/blob/master/manual/cops_performance.md#performancecasewhensplat
require 'benchmark_driver'
output = :gruff
versions = ['2.3.8', '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
BAR = 2
BAZ = 3
FOOBAR = 4
CONDITION = [1, 2, 3, 4]
def bad_sample1
foo = BAZ
case foo
when *CONDITION
BAR
when BAZ
FOOBAR
end
end
def bad_sample2
foo = 5
case foo
when *[1, 2, 3, 4]
BAR
when 5
FOOBAR
end
end
def good_sample1
foo = BAZ
case foo
when BAZ
FOOBAR
when *CONDITION
BAR
end
end
def good_sample2
foo = 5
case foo
when 1, 2, 3, 4
BAR
when 5
BAZ
end
end
RUBY
x.report %{ bad_sample1 }
x.report %{ bad_sample2 }
x.report %{ good_sample1 }
x.report %{ good_sample2 }
end