Skip to content

Instantly share code, notes, and snippets.

@keymastervn
Created February 24, 2022 07:26
Show Gist options
  • Save keymastervn/29dfb1e88cb8bca9662dab46bf07890d to your computer and use it in GitHub Desktop.
Save keymastervn/29dfb1e88cb8bca9662dab46bf07890d to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
Benchmark.ips do |b|
hash = {
:type => "Employee",
:first_name => "Mr First",
:last_name => "Last Name",
:account_email => "[email protected]",
:employing_entity => "XXX Property Services Pty Ltd",
:employee_code => nil,
:location => "Head Office NSW",
:primary_cost_centre => "XXX Property - HQ / Head Office - NSW",
:additional_cost_centres => nil,
:employment_type => "Full-time",
:job_title => "CFO",
:team => "Head Office NSW | CEO Direct reports | Executive Team | Finance & Technology | Head Office | Head Office NSW & ACT",
:primary_manager_email => "[email protected]",
:secondary_manager_email => nil,
:start_date => "17/12/2020",
:termination_date => nil,
:length_of_probation => nil,
:company_email => "[email protected]",
:company_mobile => nil,
:company_landline => nil
}
symbolized_keys = hash.keys
stringified_keys = hash.keys.map(&:to_s)
require 'active_support/core_ext/hash/indifferent_access'
b.report("convert keys symbolized") do
symbolized_keys.each {|k| hash[k.to_sym]}
end
b.report("convert keys stringified") do
stringified_keys.each {|k| hash[k.to_sym]}
end
b.report("HWIA") do
hash = hash.with_indifferent_access
symbolized_keys.each {|k| hash[k]}
end
b.compare!
end
@keymastervn
Copy link
Author

Warming up --------------------------------------
convert keys symbolized
                        83.058k i/100ms
convert keys stringified
                        46.652k i/100ms
                HWIA    10.507k i/100ms
Calculating -------------------------------------
convert keys symbolized
                        176.877k (± 0.6%) i/s -    913.638k in   5.165581s
convert keys stringified
                        148.891k (± 2.3%) i/s -    746.432k in   5.015988s
                HWIA    106.709k (± 1.9%) i/s -    535.857k in   5.023481s

Comparison:
convert keys symbolized:   176877.4 i/s
convert keys stringified:   148890.9 i/s - 1.19x  (± 0.00) slower
                HWIA:   106709.2 i/s - 1.66x  (± 0.00) slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment