Created
June 9, 2019 08:42
-
-
Save danini-the-panini/a5aaecdb0004032cfd5c56788ccd4177 to your computer and use it in GitHub Desktop.
Benchmark JSON parsing with symbol keys
This file contains 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 'active_support/core_ext/hash' | |
require 'benchmark/ips' | |
require 'json' | |
N = 1000 | |
M = 100 | |
TEST_ARRAY = N.times.map do |n| | |
h = { 'some_key' => "H #{n}" } | |
M.times do |m| | |
h = { h: h } | |
end | |
end | |
TEST_HASH = { array: TEST_ARRAY } | |
TEST_JSON = TEST_HASH.to_json | |
Benchmark.ips do |x| | |
x.report("JSON.parse(symbolize_names: true)") do | |
JSON.parse(TEST_JSON, symbolize_names: true) | |
end | |
x.report("JSON.parse then deep_symbolize_keys") do | |
JSON.parse(TEST_JSON).deep_symbolize_keys | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment