Benchmarking YAML and JSON serialisation of simple data structure in Ruby

← Back

For the purpose of ActiveRecord serialisation of a complex Hash structure into a text attribute, I was considering whether to use the default YAML serialisation or whether to switch to JSON serialisation. For my purposes, both would provide the same functionality, although YAML would allow me to use symbols, while JSON would force me to use strings. Not a big deal. So I went for a simple benchmark to see whether there is any difference in their performance.

Benchmarking script:

require 'yaml'

data = {
  :hello => {
    'foo' => 'bar',
    'fee' => 'bee',
    'fii' => 'boo',
    'fuu' => 'baa',
    'faa' => 'bii',
    'fyy' => 'byy',
  },
  :xyz => 12345678,
  :zxy => :blum,
  :yzx => 'foo bar baz nah',
}

1_000_000.times do
  YAML.dump(data)
end

For JSON, I only changed require 'json' on the top and JSON.dump(data) at the bottom. Here are my results:

$ time ruby json.rb

real	0m2,232s
user	0m2,229s
sys	0m0,004s

$ time ruby yaml.rb

real	1m2,513s
user	1m2,502s
sys	0m0,010s

(Please note the difference in 0m and 1m.)

I didn’t quite expect such a difference. It’s true that YAML implementation in Ruby is able to serialise most Ruby objects, which adds some overhead. But this is a lot. Luckily, I don’t need any extra features and can do with JSON serialisation just fine, so I didn’t need any further thoughts to make my choice.


Tagged with: Ruby, benchmark

Written: 2022-03-24