Skip to content

Commit

Permalink
Merge pull request #200 from wasaylor/constantize-vs-comparison
Browse files Browse the repository at this point in the history
String#constantize vs simple comparison
  • Loading branch information
lubc authored Jun 24, 2022
2 parents e8ac915 + c0ac0fe commit e860f60
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ Comparison:
module_eval with string: 1129.7 i/s - 1.19x slower
```

##### `String#constantize` vs a comparison for inflection

ActiveSupport's [String#constantize](https://guides.rubyonrails.org/active_support_core_extensions.html#constantize) "resolves the constant reference expression in its receiver".

[Read the rationale here](https://github.com/fastruby/fast-ruby/pull/200)

```
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-darwin20]
Calculating -------------------------------------
using an if statement
8.124M (± 1.8%) i/s - 41.357M in 5.092437s
String#constantize 2.462M (± 2.4%) i/s - 12.315M in 5.004089s
Comparison:
using an if statement: 8123851.3 i/s
String#constantize: 2462371.2 i/s - 3.30x (± 0.00) slower
```

##### `raise` vs `E2MM#Raise` for raising (and defining) exeptions [code](code/general/raise-vs-e2mmap.rb)

Ruby's [Exception2MessageMapper module](http://ruby-doc.org/stdlib-2.2.0/libdoc/e2mmap/rdoc/index.html) allows one to define and raise exceptions with predefined messages.
Expand Down
20 changes: 20 additions & 0 deletions code/general/constantize-vs-comparison.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "active_support/core_ext/string/inflections.rb"
require "benchmark/ips"

class Foo; end

def fast(s)
klass = Foo if s == "Foo"
nil
end

def slow(s)
klass = s.constantize
nil
end

Benchmark.ips do |x|
x.report("using an if statement") { fast("Foo") }
x.report("String#constantize") { slow("Foo") }
x.compare!
end

0 comments on commit e860f60

Please sign in to comment.