{{ message }}
Repository navigation
String#=~ faster than String#match - #59
Merged
JuanitoFatas merged 1 commit intoAug 7, 2015
Merged
Conversation
If you're only looking for presence of a match and don't need the matchdata, use String#=~ schneems/rails@1bf50ba#commitcomment-12572839
schneems
referenced
this pull request
in schneems/rails
Aug 6, 2015
In `apply_inflections` a string is down cased and some whitespace stripped in the front (which allocate strings). This would normally be fine, however `uncountables` is a fairly small array (10 elements out of the box) and this method gets called a TON. Instead we can keep an array of valid regexes for each uncountable so we don't have to allocate new strings. This change buys us 325,106 bytes of memory and 3,251 fewer objects per request.
Collaborator
Contributor
Author
|
Yep Seems good. How should I report this? I would like to have all 3 examples in the code and reported. |
Collaborator
|
Edited my comment as String#=== was wrong, I meant Regexp#=== |
Contributor
|
How about list all possibilities ( require "benchmark/ips"
def fastest
"foo".freeze === /boo/
end
def faster
"foo".freeze =~ /boo/
end
def fast
/boo/ === "foo".freeze
end
def slow
"foo".freeze.match(/boo/)
end
Benchmark.ips do |x|
x.report("String#===") { fastest }
x.report("Regexp#===") { faster }
x.report("String#=~") { fast }
x.report("String#match") { slow }
x.compare!
end❓ |
Collaborator
|
String#=== doesn't work, it's not the correct result. |
Contributor
Yup. The #59 (comment) is wrong. Let's keep this simple since the regexp need to flip the string to be compared to the end. Which is counterintuitive |
JuanitoFatas
added a commit
that referenced
this pull request
Aug 7, 2015
String#=~ faster than String#match
schneems
added a commit
to schneems/rails
that referenced
this pull request
Aug 7, 2015
Contributor
Author
JuanitoFatas
added a commit
that referenced
this pull request
Nov 4, 2015
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

If you're only looking for presence of a match and don't need the matchdata, use String#=~
schneems/rails@1bf50ba#commitcomment-12572839