Created
January 13, 2017 22:51
-
-
Save yuki24/48f28563e80ad8cc1c6e2ac65ede0722 to your computer and use it in GitHub Desktop.
JSON with the symbolize_name: true option
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 'json' | |
require 'open-uri' | |
require 'benchmark/ips' | |
require 'memory_profiler' | |
json = open('https://network.pivotal.io/api/v2/products').read | |
Benchmark.ips do |x| | |
x.report('symbolize_names: false') { JSON.parse(json) } | |
x.report('symbolize_names: true') { JSON.parse(json, symbolize_names: true) } | |
x.compare! | |
end | |
puts "\n\n\n" | |
puts 'symbolize_names: false (default)' | |
puts '-----------------------------------' | |
MemoryProfiler.report do | |
100.times { JSON.parse(json) } | |
end.pretty_print | |
GC.start | |
puts "\n\n\n" | |
puts 'symbolize_names: true' | |
puts '-----------------------------------' | |
MemoryProfiler.report do | |
100.times { JSON.parse(json, symbolize_names: true) } | |
end.pretty_print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result:
symbolize_names: false (default)
Total allocated: 38994500 bytes (286200 objects)
Total retained: 0 bytes (0 objects)
allocated memory by gem
38994500 2.3.3/lib
allocated memory by file
38994500 /Users/pivotal/.rbenv/versions/2.3.3/lib/ruby/2.3.0/json/common.rb
allocated memory by location
38990500 /Users/pivotal/.rbenv/versions/2.3.3/lib/ruby/2.3.0/json/common.rb:156
4000 /Users/pivotal/.rbenv/versions/2.3.3/lib/ruby/2.3.0/json/common.rb:155
allocated memory by class
28570500 String
10235200 Hash
116800 JSON::Ext::Parser
72000 Array
allocated objects by gem
allocated objects by file
allocated objects by location
allocated objects by class
retained memory by gem
NO DATA
retained memory by file
NO DATA
retained memory by location
NO DATA
retained memory by class
NO DATA
retained objects by gem
NO DATA
retained objects by file
NO DATA
retained objects by location
NO DATA
retained objects by class
NO DATA
Allocated String Report
Retained String Report
symbolize_names: true
Total allocated: 35226180 bytes (191032 objects)
Total retained: 320 bytes (8 objects)
allocated memory by gem
35202980 2.3.3/lib
23200 other
allocated memory by file
35202980 /Users/pivotal/.rbenv/versions/2.3.3/lib/ruby/2.3.0/json/common.rb
23200 json_with_synbolize_names.rb
allocated memory by location
35202980 /Users/pivotal/.rbenv/versions/2.3.3/lib/ruby/2.3.0/json/common.rb:156
23200 json_with_synbolize_names.rb:29
allocated memory by class
24759460 String
10277600 Hash
116800 JSON::Ext::Parser
72000 Array
320 Symbol
allocated objects by gem
allocated objects by file
allocated objects by location
allocated objects by class
retained memory by gem
retained memory by file
retained memory by location
retained memory by class
retained objects by gem
retained objects by file
retained objects by location
retained objects by class
Allocated String Report
Retained String Report