-
-
Save ang3lkar/98cab34117f9f6ad56d17902f34e88eb to your computer and use it in GitHub Desktop.
BENCHMARKS: How to create fast JSON serialization in Rails?
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
# 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 } | |
} |
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
# 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 } | |
} |
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
# 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}) } | |
} |
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
# 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]) } | |
} |
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
# 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']) } | |
} |
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
# 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