Skip to content

Prefer match? over =~ to avoid MatchData#8138

Merged
tagliala merged 1 commit into
activeadmin:masterfrom
tagliala:chore/use-match-to-avoid-matchdata
Nov 17, 2023
Merged

Prefer match? over =~ to avoid MatchData#8138
tagliala merged 1 commit into
activeadmin:masterfrom
tagliala:chore/use-match-to-avoid-matchdata

Conversation

@tagliala

Copy link
Copy Markdown
Contributor

When the specific MatchData object is not needed, using regexp.match? is preferred over =~ as it avoids the creation of a MatchData object, resulting in improved performance and reduced memory usage.

Example:

PARTS = ["admin", "users", "2b2f0fc2-9a0d-41b8-b39d-aa21963aaee4"]

%i[ips memory].each do |benchmark|
  Benchmark.send(benchmark) do |x|
    x.report('=~') { PARTS.each { |part| part =~ ID_FORMAT_REGEXP } }
    x.report('match?') { PARTS.each { |part| ID_FORMAT_REGEXP.match?(part) } }
    x.compare!
  end
end
Comparison (IPS):
              match?:   943080.8 i/s
                  =~:   621955.5 i/s - 1.52x  (± 0.00) slower

Comparison (Memory):
              match?:          0 allocated
                  =~:        168 allocated - Infx more

When the specific `MatchData` object is not needed, using
`regexp.match?` is preferred over `=~` as it avoids the creation of a
MatchData object, resulting in improved performance and reduced memory
usage.

Example:

```rb
PARTS = ["admin", "users", "2b2f0fc2-9a0d-41b8-b39d-aa21963aaee4"]

%i[ips memory].each do |benchmark|
  Benchmark.send(benchmark) do |x|
    x.report('=~') { PARTS.each { |part| part =~ ID_FORMAT_REGEXP } }
    x.report('match?') { PARTS.each { |part| ID_FORMAT_REGEXP.match?(part) } }
    x.compare!
  end
end
```

```
Comparison (IPS):
              match?:   943080.8 i/s
                  =~:   621955.5 i/s - 1.52x  (± 0.00) slower

Comparison (Memory):
              match?:          0 allocated
                  =~:        168 allocated - Infx more
```
@codecov

codecov Bot commented Nov 17, 2023

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (41c24aa) 99.05% compared to head (2652e21) 99.05%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8138   +/-   ##
=======================================
  Coverage   99.05%   99.05%           
=======================================
  Files         184      184           
  Lines        4745     4746    +1     
=======================================
+ Hits         4700     4701    +1     
  Misses         45       45           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@javierjulio javierjulio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Odd that the Performance/RegexpMatch rule didn't pick this up.

@tagliala tagliala merged commit ccdf0d7 into activeadmin:master Nov 17, 2023
@tagliala tagliala deleted the chore/use-match-to-avoid-matchdata branch November 17, 2023 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants