Commit 80f3a44
authored
rubocop: reduce string to symbol conversion for performance (#4930)
**Which issue(s) this PR fixes**:
Fixes #
**What this PR does / why we need it**:
This is cosmetic change, it does not change behavior at all. It was
detected by the following rubocop configuration:
```
Performance/StringIdentifierArgument:
Enabled: true
```
Apparently, string identifier argument should not be used.
Benchmark result:
```
ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux]
Warming up --------------------------------------
check method with string notation
875.323k i/100ms
check method with symbol notation
1.880M i/100ms
Calculating -------------------------------------
check method with string notation
8.828M (± 1.4%) i/s (113.28 ns/i) - 44.641M in 5.058002s
check method with symbol notation
19.766M (± 2.2%) i/s (50.59 ns/i) - 99.631M in 5.043071s
Comparison:
check method with symbol notation: 19765521.3 i/s
check method with string notation: 8827598.1 i/s - 2.24x slower
```
Appendix: benchmark script
```ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'benchmark-memory'
end
Benchmark.ips do |x|
x.report("check method with string notation") {
"test".respond_to?("start_with?")
}
x.report("check method with symbol notation") {
"test".respond_to?(:start_with?)
}
x.compare!
end
```
**Docs Changes**:
N/A
**Release Note**:
N/A
Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>1 parent 54c9864 commit 80f3a44
2 files changed
+2
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
178 | | - | |
| 178 | + | |
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
| |||
0 commit comments