Skip to content

Instantly share code, notes, and snippets.

@mjc
Forked from jurre/bench.rb
Created Nov 20, 2015
Embed
What would you like to do?
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment