Skip to content

Instantly share code, notes, and snippets.

@deadkarma
Last active August 29, 2015 14:11
Show Gist options
  • Save deadkarma/63ef3be0e6598f414346 to your computer and use it in GitHub Desktop.
Save deadkarma/63ef3be0e6598f414346 to your computer and use it in GitHub Desktop.
Multi-JSON vs JSON vs Messagepack
require 'benchmark/ips'
require 'multi_json'
require 'json'
require 'msgpack'
hash = {
"site_config"=>
{
"created_at"=>"2014-03-24T19:58:25Z",
"data"=>"",
"data_value"=>"1",
"group_id"=>nil,
"id"=>143,
"site_config_type_id"=>40500,
"site_id"=>1,
"updated_at"=>"2014-04-24T19:57:27Z",
"user_id"=>nil
}
}
Benchmark.ips do |x|
x.report('multi-json') { MultiJson.load(hash.to_json) }
x.report('json') { JSON.parse(hash.to_json) }
x.report('msgpack') { MessagePack.unpack(hash.to_msgpack) }
x.compare!
end
@deadkarma
Copy link
Author

Calculating -------------------------------------
          multi-json     4.416k i/100ms
                json     5.037k i/100ms
             msgpack    12.149k i/100ms
-------------------------------------------------
          multi-json     46.020k (± 6.5%) i/s -    229.632k
                json     53.548k (± 6.7%) i/s -    271.998k
             msgpack    135.581k (± 6.6%) i/s -    680.344k

Comparison:
             msgpack:   135580.8 i/s
                json:    53547.6 i/s - 2.53x slower
          multi-json:    46020.2 i/s - 2.95x slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment