Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created December 17, 2011 14:07
Show Gist options
  • Save passingloop/1490289 to your computer and use it in GitHub Desktop.
Save passingloop/1490289 to your computer and use it in GitHub Desktop.
yaml vs json
require 'benchmark'
require 'yaml'
require 'rubygems'
require 'bundler'
Bundler.require(:default)
require 'active_support'
def token
[Array.new(24) { rand(256).chr }.join].pack('m*').chop
end
puts "token length: #{token.size}"
data = {}
50.times do
data[token] = Array.new(3) { token }
end
yaml = YAML.dump(data)
puts "YAML size: #{yaml.size}"
json = ActiveSupport::JSON.encode(data)
puts "JSON size: #{json.size}"
puts "JSON engine: #{ActiveSupport::JSON.engine}"
n = 1000
Benchmark.bmbm do |x|
GC.disable
x.report('YAML.dump') { n.times do; YAML.dump(data); end }
x.report('YAML.load') { n.times do; YAML.load(yaml); end}
x.report('JSON.encode') { n.times do; ActiveSupport::JSON.encode(data); end }
x.report('JSON.decode') { n.times do; ActiveSupport::JSON.decode(json); end }
GC.enable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment