Last active
August 11, 2017 12:31
-
-
Save NullVoxPopuli/2cfbc5c28e2170530b25f279b7e8375d to your computer and use it in GitHub Desktop.
Ruby Hash Access Comparisons
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
require 'benchmark/ips' | |
require "active_support/core_ext/hash/indifferent_access" | |
symbols = { | |
a: 1 | |
} | |
strings = { | |
'a'.freeze => 1 | |
} | |
un_frozen_strings = { | |
"a" => 1 | |
} | |
indifferent = { | |
a: 1, | |
'b'.freeze => 2, | |
"c" => 3 | |
}.with_indifferent_access | |
Benchmark.ips do |x| | |
x.config(time: 5, warmup: 2, iterations: 1) | |
x.report('symbols') { symbols[:a] } | |
x.report('string') { strings['a'] } | |
x.report('unfrozen strings') { strings["a"] } | |
x.report('indifferent - string -> symbol') { indifferent['a'] } | |
x.report('indifferent - symbol -> string') { indifferent[:b] } | |
x.report('indifferent - symbol -> unfrozen') { indifferent[:c] } | |
x.report('indifferent - symbol -> symbol') { indifferent[:a] } | |
x.report('indifferent - string -> string') { indifferent['b'] } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
System:
MacBookPro11,5
16GiB System Memory
Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz