Skip to content

Instantly share code, notes, and snippets.

@mimosa
Last active May 28, 2018 03:12
Show Gist options
  • Save mimosa/ac14b3527c33bf767dc11875ce3964b8 to your computer and use it in GitHub Desktop.
Save mimosa/ac14b3527c33bf767dc11875ce3964b8 to your computer and use it in GitHub Desktop.
roar vs ams vs rabl vs jbuilder
require 'bundler'
require 'active_model_serializers'
require 'roar/decorator'
require 'roar/json'
require 'rabl'
require 'jbuilder'
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: FFaker::Name.name, body: FFaker::HipsterIpsum.paragraph, draft: [true, false].sample)
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :author, :body, :draft
end
module PostsRepresenter
include Roar::JSON
property :id
property :author
property :body
property :draft
end
def rabl_template
'
collection @posts
attributes :id, :author, :body, :draft
'
end
puts '_' * 88
def ams_test(posts)
ActiveModel::Serializer::CollectionSerializer.new(posts, each_serializer: PostSerializer).to_json
end
def roar_test(posts)
PostsRepresenter.for_collection.prepare(posts).to_json
end
def rabl_test(posts)
Rabl.render(posts, rabl_template)
end
def jbuilder_test(posts)
Jbuilder.encode do |json|
json.array! posts, :id, :author, :body, :draft
end
end
puts 'with 100_000 records'
Benchmark.bm do |x|
x.report('AMS') { ams_test(posts) }
x.report('Roar') { roar_test(posts) }
x.report('RABL') { rabl_test(posts) }
x.report('JB') { jbuilder_test(posts) }
end
puts "with 2000 records"
first2k = posts.take(2000)
Benchmark.bm do |x|
x.report('AMS') { ams_test(first2k) }
x.report('Roar') { roar_test(first2k) }
x.report('RABL') { rabl_test(first2k) }
x.report('JB') { jbuilder_test(first2k) }
end
with 100_000 records
user system total real
AMS 8.570000 0.220000 8.790000 ( 8.966108)
Roar 12.940000 0.250000 13.190000 ( 13.522623)
RABL 3.400000 0.080000 3.480000 ( 3.488494)
JB 2.420000 0.040000 2.460000 ( 2.479700)
with 2000 records
user system total real
AMS 0.130000 0.010000 0.140000 ( 0.133761)
Roar 0.140000 0.000000 0.140000 ( 0.146554)
RABL 0.030000 0.000000 0.030000 ( 0.033616)
JB 0.030000 0.000000 0.030000 ( 0.025291)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment