Skip to content

Instantly share code, notes, and snippets.

@jurre
Created January 13, 2015 10:51
Show Gist options
  • Save jurre/ac0cb38540a4a2190ada to your computer and use it in GitHub Desktop.
Save jurre/ac0cb38540a4a2190ada to your computer and use it in GitHub Desktop.
roar vs ams
require "bundler"
require "active_model_serializers"
require "roar"
require "roar/json/json_api"
require "benchmark"
require "ffaker"
Post = Struct.new(:id, :author, :body, :draft) do
include ActiveModel::Serializers::JSON
end
posts = []
(1..100_000).each do |id|
posts << Post.new(id: id, author: Faker::Name.name, body: Faker::HipsterIpsum.paragraph, draft: [true, false].sample)
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :author, :body, :draft
end
module PostsRepresenter
include Roar::JSON::JSONAPI
type :posts
property :id
property :author
property :body
property :draft
end
puts "with 100_000 records"
Benchmark.bm do |x|
x.report("ActiveModel::Serializer") { ActiveModel::ArraySerializer.new(posts, each_serializer: PostSerializer).to_json }
x.report("Roar::JSON::JSONAPI") { PostsRepresenter.for_collection.prepare(posts).to_json }
end
puts "with 2000 records"
first2k = posts.take(2000)
Benchmark.bm do |x|
x.report("ActiveModel::Serializer") { ActiveModel::ArraySerializer.new(first2k, each_serializer: PostSerializer).to_json }
x.report("Roar::JSON::JSONAPI") { PostsRepresenter.for_collection.prepare(first2k).to_json }
end
with 100_000 records
user system total real
ActiveModel::Serializer 6.000000 0.130000 6.130000 ( 6.168347)
Roar::JSON::JSONAPI 20.710000 0.240000 20.950000 ( 21.055724)
with 2000 records
user system total real
ActiveModel::Serializer 0.100000 0.010000 0.110000 ( 0.102477)
Roar::JSON::JSONAPI 0.220000 0.000000 0.220000 ( 0.219107)
@mimosa
Copy link

mimosa commented Feb 22, 2017

with 100_000 records
       user     system      total        real
AMS  8.270000   0.090000   8.360000 (  8.369318)
Roar 10.230000   0.050000  10.280000 ( 10.308165)
RABL  1.830000   0.020000   1.850000 (  1.854693)
with 2000 records
       user     system      total        real
AMS  0.140000   0.000000   0.140000 (  0.140841)
Roar  0.160000   0.010000   0.170000 (  0.161813)
RABL  0.030000   0.000000   0.030000 (  0.033712)

RABL is better 👍

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