Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BENCHMARKS: How to create fast JSON serialization in Rails?
# post_serializer.rb
class PostSerializer < ActiveModel::Serializer
has_many :comments
attributes :id, :title, :content
end
# comment_serializer.rb
class CommentSerializer < ActiveModel::Serializer
attributes :id, :body, :author
end
# Benchmark
posts = Post.eager_load(:comments).all
puts Benchmark.measure {
posts.map { |post| PostSerializer.new(post).as_json }
}
# post_serializer.rb
class PostSerializer
include FastJsonapi::ObjectSerializer
attributes :title, :content
has_many :comments
end
# comment_serializer.rb
class CommentSerializer
include FastJsonapi::ObjectSerializer
attributes :id, :body, :author
end
# Benchmark
posts = Post.eager_load(:comments).all
puts Benchmark.measure {
posts.map { |post| PostSerializer.new(post, include: [:comments]).serializable_hash }
}
# post2.json.jbuilder
json.id post.id
json.title post.title
json.content post.content
json.comments(post.comments) do |comment|
json.id comment.id
json.author comment.author
json.body comment.body
end
# Benchmark
posts = Post.eager_load(:comments).all
renderer = ApplicationController.new
puts Benchmark.measure {
posts.map { |post| renderer.render_to_string('/post2', locals: {post: post}) }
}
# post_serializer.rb
class PostSerializer < JSONAPI::Serializable::Resource
type 'posts'
has_many :comments
attributes :id, :title, :content
end
# comment_serializer.rb
class CommentSerializer < JSONAPI::Serializable::Resource
type 'comments'
attributes :id, :author, :body
end
# Benchmark
posts = Post.eager_load(:comments).all
renderer = JSONAPI::Serializable::Renderer.new
puts Benchmark.measure {
posts.map { |post| renderer.render(post, class: { Post: PostSerializer, Comment: CommentSerializer }, include: [:comments]) }
}
# post_resource.rb
class PostResource < JSONAPI::Resource
has_many :comments
attributes :title, :content
end
# comment_resource.rb
class CommentResource < JSONAPI::Resource
attributes :author, :body
end
# Benchmark
posts = Post.eager_load(:comments).all
puts Benchmark.measure {
posts.map { |post| JSONAPI::ResourceSerializer.new(PostResource, include: ['comments']).serialize_to_hash(PostResource.new(post, nil)) }
}
# post_serializer.rb
class PostSerializer
include JSONAPI::Serializer
attribute :id
attribute :title
attribute :content
has_many :comments
end
# comment_serializer.rb
class CommentSerializer
include JSONAPI::Serializer
attribute :id
attribute :author
attribute :body
end
# Benchmark
posts = Post.eager_load(:comments).all
puts Benchmark.measure {
posts.map { |post| JSONAPI::Serializer.serialize(post, include: ['comments']) }
}
# post.rabl
object @job
attributes :id, :title, :content
child :comments do
extends "comment"
end
# comment.rabl
object @comment
attributes :id, :author, :body
# Benchmark
posts = Post.eager_load(:comments).all
puts Benchmark.measure {
posts.map { |post| Rabl.render(post, 'post', :view_path => 'app/views', :format => :hash) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.