Skip to content

Instantly share code, notes, and snippets.

@luislavena
Created September 5, 2015 15:53
Show Gist options
  • Save luislavena/365157467afa36419025 to your computer and use it in GitHub Desktop.
Save luislavena/365157467afa36419025 to your computer and use it in GitHub Desktop.
require "benchmark/ips"
require "granola"
require "json"
require "oj"
require "yajl"
require "multi_json"
require "multi_json/adapters/json_gem"
require "multi_json/adapters/oj"
require "multi_json/adapters/yajl"
Oj.default_options = { mode: :compat }
User = Struct.new(:name, :age)
class UserSerializer < Granola::Serializer
def data
{ "name" => object.name, "age" => object.age }
end
end
SERIALIZER = UserSerializer.new(User.new("John Doe", 30))
JSON_METHOD = JSON.method(:generate)
OJ_METHOD = Oj.method(:dump)
YAJL_METHOD = Yajl::Encoder.method(:encode)
MULTI_METHOD = MultiJson.method(:dump)
Benchmark.ips do |b|
b.report("json") do
Granola.json = JSON_METHOD
SERIALIZER.to_json
end
b.report("oj") do
Granola.json = OJ_METHOD
SERIALIZER.to_json
end
b.report("yajl") do
Granola.json = YAJL_METHOD
SERIALIZER.to_json
end
b.report("multi_json/json") do
MultiJson.engine = MultiJson::Adapters::JsonGem
Granola.json = MULTI_METHOD
SERIALIZER.to_json
end
b.report("multi_json/oj") do
MultiJson.engine = MultiJson::Adapters::Oj
Granola.json = MULTI_METHOD
SERIALIZER.to_json
end
b.report("multi_json/yajl") do
MultiJson.engine = MultiJson::Adapters::Yajl
Granola.json = MULTI_METHOD
SERIALIZER.to_json
end
b.compare!
end
Calculating -------------------------------------
json 9.352k i/100ms
oj 21.870k i/100ms
yajl 9.232k i/100ms
multi_json/json 7.167k i/100ms
multi_json/oj 9.979k i/100ms
multi_json/yajl 6.656k i/100ms
-------------------------------------------------
json 113.833k (± 8.2%) i/s - 570.472k
oj 348.320k (±23.5%) i/s - 1.662M
yajl 117.316k (±24.3%) i/s - 563.152k
multi_json/json 81.048k (± 9.9%) i/s - 408.519k
multi_json/oj 124.795k (±11.2%) i/s - 618.698k
multi_json/yajl 79.713k (±21.1%) i/s - 386.048k
Comparison:
oj: 348320.1 i/s
multi_json/oj: 124794.7 i/s - 2.79x slower
yajl: 117316.0 i/s - 2.97x slower
json: 113833.2 i/s - 3.06x slower
multi_json/json: 81047.7 i/s - 4.30x slower
multi_json/yajl: 79713.5 i/s - 4.37x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment