Skip to content

Instantly share code, notes, and snippets.

@groyoh
Created May 16, 2016 19:46
Show Gist options
  • Save groyoh/22583d208b1284a092fb6a58e18be1ec to your computer and use it in GitHub Desktop.
Save groyoh/22583d208b1284a092fb6a58e18be1ec to your computer and use it in GitHub Desktop.
Static vs Dynamic underscore
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activesupport"
gem "benchmark-ips", require: "benchmark/ips"
end
require "active_support/all"
FOOBAR = "foo_bar".freeze
MAPPING = {
"FooBar" => FOOBAR,
"fooBar" => FOOBAR,
"Foo_Bar" => FOOBAR
}.freeze
Benchmark.ips do |x|
x.warmup = 20
x.report("underscore") do
"FooBar".underscore
"fooBar".underscore
"Foo_Bar".underscore
end
x.report("mapping") do
MAPPING["FooBar"]
MAPPING["fooBar"]
MAPPING["Foo_Bar"]
end
x.compare!
end
__END__
Warming up --------------------------------------
underscore 6.790k i/100ms
mapping 150.724k i/100ms
Calculating -------------------------------------
underscore 77.864k (± 2.2%) i/s - 393.820k in 5.060335s
mapping 3.660M (± 2.9%) i/s - 18.388M in 5.029111s
Comparison:
mapping: 3659726.5 i/s
underscore: 77863.6 i/s - 47.00x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment