Skip to content

Commit 69c1ed1

Browse files
committed
Add Regexp#=== to match and =~ comparison
Closes #59
1 parent 414f86f commit 69c1ed1

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -786,28 +786,36 @@ Comparison:
786786
String#=~: 891124.1 i/s - 3.30x slower
787787
```
788788

789-
##### `String#match` vs `String#=~` [code ](code/string/match-vs-=~.rb)
789+
##### `Regexp#===` vs `String#match` vs `String#=~` [code ](code/string/===-vs-=~-vs-match.rb)
790790

791791
> :warning: <br>
792792
> Sometimes you can't replace `match` with `=~`, <br>
793793
> This is only useful for cases where you are checking <br>
794-
> for a match and not using the resultant match object.
794+
> for a match and not using the resultant match object. <br>
795+
> :warning: <br>
796+
> `Regexp#===` is also faster than `String#match` but you need to switch the order of arguments.
795797
796798
```
797-
$ ruby -v code/string/match-vs-=~.rb
799+
$ ruby -v code/string/===-vs-=~-vs-match.rb.rb
798800
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
801+
799802
Calculating -------------------------------------
800-
String#=~ 69.889k i/100ms
801-
String#match 66.715k i/100ms
803+
String#=~ 98.184k i/100ms
804+
Regexp#=== 92.382k i/100ms
805+
String#match 83.601k i/100ms
802806
-------------------------------------------------
803-
String#=~ 1.854M (±12.2%) i/s - 9.155M
804-
String#match 1.594M (±11.0%) i/s - 7.939M
807+
String#=~ 2.442M (± 7.6%) i/s - 12.175M
808+
Regexp#=== 2.259M (± 7.9%) i/s - 11.271M
809+
String#match 1.840M (± 7.3%) i/s - 9.196M
805810
806811
Comparison:
807-
String#=~: 1853861.7 i/s
808-
String#match: 1593971.6 i/s - 1.16x slower
812+
String#=~: 2442335.1 i/s
813+
Regexp#===: 2259277.3 i/s - 1.08x slower
814+
String#match: 1839815.4 i/s - 1.33x slower
809815
```
810816

817+
See [#59](https://github.com/JuanitoFatas/fast-ruby/pull/59) for discussions.
818+
811819

812820
##### `String#gsub` vs `String#sub` [code](code/string/gsub-vs-sub.rb)
813821

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
require 'benchmark/ips'
1+
require "benchmark/ips"
22

3-
def fast
3+
def fastest
44
"foo".freeze =~ /boo/
55
end
66

7+
def fast
8+
/boo/ === "foo".freeze
9+
end
10+
711
def slow
812
"foo".freeze.match(/boo/)
913
end
1014

1115
Benchmark.ips do |x|
12-
x.report("String#=~") { fast }
16+
x.report("String#=~") { fastest }
17+
x.report("Regexp#===") { fast }
1318
x.report("String#match") { slow }
1419
x.compare!
1520
end

0 commit comments

Comments
 (0)