Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active June 6, 2018 06:02
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 havenwood/c4a9d527df030dfaab996d42f745600b to your computer and use it in GitHub Desktop.
Save havenwood/c4a9d527df030dfaab996d42f745600b to your computer and use it in GitHub Desktop.
benchmark for json, yajl and oj.
~ ruby json_benchmark.rb
String to JSON
Warming up --------------------------------------
Oj.dump 24.918k i/100ms
Yajl.dump 9.461k i/100ms
JSON.dump 24.797k i/100ms
MessagePack.dump 44.355k i/100ms
Calculating -------------------------------------
Oj.dump 267.683k (± 3.4%) i/s - 1.346M in 5.032971s
Yajl.dump 98.929k (± 2.8%) i/s - 501.433k in 5.072850s
JSON.dump 267.699k (± 3.4%) i/s - 1.339M in 5.008431s
MessagePack.dump 506.836k (± 3.1%) i/s - 2.573M in 5.080857s
Comparison:
MessagePack.dump: 506835.6 i/s
JSON.dump: 267699.2 i/s - 1.89x slower
Oj.dump: 267683.0 i/s - 1.89x slower
Yajl.dump: 98928.5 i/s - 5.12x slower
JSON to String
Warming up --------------------------------------
Oj.load 6.843k i/100ms
Yajl.load 4.075k i/100ms
JSON.parse 4.739k i/100ms
MessagePack.load 9.835k i/100ms
Calculating -------------------------------------
Oj.load 71.835k (± 2.6%) i/s - 362.679k in 5.052311s
Yajl.load 41.681k (± 2.5%) i/s - 211.900k in 5.087144s
JSON.parse 46.690k (± 4.9%) i/s - 236.950k in 5.087611s
MessagePack.load 100.421k (± 4.5%) i/s - 501.585k in 5.005654s
Comparison:
MessagePack.load: 100420.7 i/s
Oj.load: 71835.4 i/s - 1.40x slower
JSON.parse: 46690.3 i/s - 2.15x slower
Yajl.load: 41681.5 i/s - 2.41x slower
require 'benchmark/ips'
require 'json'
require 'oj'
require 'yajl'
require 'msgpack'
HASH = {"audios_count"=>0, "audios_updated_at"=>nil, "avatar_content_type"=>nil, "avatar_file_name"=>nil, "avatar_file_size"=>nil, "avatar_updated_at"=>nil, "backend_id"=>nil, "cached_average_listen_span"=>0.0, "cached_followers_count"=>1, "cached_followings_count"=>1, "cached_likes_count"=>1, "city"=>nil, "country_code"=>nil, "country_name"=>nil, "cover_content_type"=>nil, "cover_file_name"=>nil, "cover_file_size"=>nil, "cover_updated_at"=>nil, "created_at"=>"Thu, 17 Jan 2013 08:49:20 UTC +00:00", "description"=>nil, "email"=>"test@gmail.com", "followers_updated_at"=>"Thu, 23 May 2013 09:19:19 UTC +00:00", "followings_updated_at"=>nil, "id"=>2, "links"=>nil, "loves_updated_at"=>"Wed, 15 May 2013 16:20:40 UTC +00:00", "name"=>"Liu Fengyun", "playlists_count"=>1, "playlists_updated_at"=>nil, "provider"=>"facebook", "role"=>nil, "slug"=>"liu-fengyun", "source"=>nil, "source_id"=>nil, "state_code"=>nil, "uid"=>"11111111", "unread_notifications_count"=>5, "updated_at"=>"Fri, 24 May 2013 07:32:16 UTC +00:00", :avatar=>{:large=>"//graph.facebook.com/11111111/picture?type=large", :normal=>"//graph.facebook.com/11111111/picture?type=normal", :small=>"//graph.facebook.com/11111111/picture?type=small"}, :slug=>"liu-fengyun", :cover=>"/covers/original/missing.png"}
puts 'String to JSON'
Benchmark.ips do |x|
hash = HASH
x.report('Oj.dump') { Oj.dump hash }
x.report('Yajl.dump') { Yajl.dump hash }
x.report('JSON.dump') { Oj.dump hash }
x.report('MessagePack.dump') { MessagePack.dump hash }
x.compare!
end
puts 'JSON to String'
Benchmark.ips do |x|
json = HASH.to_json
msgpack = HASH.to_msgpack
x.report('Oj.load') { Oj.load json }
x.report('Yajl.load') { Yajl.load json }
x.report('JSON.parse') { JSON.parse json }
x.report('MessagePack.load') { MessagePack.load msgpack }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment