Skip to content

Instantly share code, notes, and snippets.

@danini-the-panini
Created June 9, 2019 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danini-the-panini/a5aaecdb0004032cfd5c56788ccd4177 to your computer and use it in GitHub Desktop.
Save danini-the-panini/a5aaecdb0004032cfd5c56788ccd4177 to your computer and use it in GitHub Desktop.
Benchmark JSON parsing with symbol keys
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