Created
January 13, 2015 10:51
-
-
Save jurre/ac0cb38540a4a2190ada to your computer and use it in GitHub Desktop.
roar vs ams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
RABL is better 👍