Uh oh!
There was an error while loading. Please reload this page.
String#=~ faster than String#match - #59
Conversation
If you're only looking for presence of a match and don't need the matchdata, use String#=~ schneems/rails@1bf50ba#commitcomment-12572839
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.
Arcovion
commented
Aug 6, 2015
Regexp#=== is a tiny bit faster on my machine, worth including: require'benchmark/ips'deffastest/boo/ === 'foo'.freezeenddeffast'foo'.freeze =~ /boo/enddefslow'foo'.freeze.match(/boo/)endBenchmark.ipsdo |bm|
bm.report("Regexp#==="){fastest}bm.report("String#=~"){fast}bm.report("String#match"){slow}bm.compare!end |
schneems
commented
Aug 6, 2015
Yep Seems good. How should I report this? I would like to have all 3 examples in the code and reported. |
Arcovion
commented
Aug 6, 2015
Edited my comment as String#=== was wrong, I meant Regexp#=== |
JuanitoFatas
commented
Aug 7, 2015
How about list all possibilities ( require"benchmark/ips"deffastest"foo".freeze === /boo/enddeffaster"foo".freeze =~ /boo/enddeffast/boo/ === "foo".freezeenddefslow"foo".freeze.match(/boo/)endBenchmark.ipsdo |x|
x.report("String#==="){fastest}x.report("Regexp#==="){faster}x.report("String#=~"){fast}x.report("String#match"){slow}x.compare!end❓ |
Arcovion
commented
Aug 7, 2015
String#=== doesn't work, it's not the correct result. |
JuanitoFatas
commented
Aug 7, 2015
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 |
String#=~ faster than String#match
schneems
commented
Aug 7, 2015
It's a pretty big speed boost, we would have to rename it to |
If you're only looking for presence of a match and don't need the matchdata, use String#=~
schneems/rails@1bf50ba#commitcomment-12572839