Last active
August 29, 2015 14:07
-
-
Save bobbytables/8a3e64eb7e9fd29617bf to your computer and use it in GitHub Desktop.
Kartograph for JSON generation is fast.
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 'benchmark/ips' | |
require 'active_support/core_ext' | |
require 'jbuilder' | |
require 'kartograph' | |
require 'active_model_serializers' | |
require 'pry' | |
User = Struct.new(:id, :name, :email) do | |
def read_attribute_for_serialization(attribute) | |
send(attribute) | |
end | |
end | |
user = User.new 1, 'Bobby', '[email protected]' | |
@users = Array.new(20, user) | |
@user = user | |
class UserMapping | |
include Kartograph::DSL | |
kartograph do | |
root_key singular: 'user', plural: 'users', scopes: [:read] | |
property :id, scopes: :read | |
property :name, scopes: :read | |
property :email, scopes: :read | |
end | |
end | |
class UserSerializer < ActiveModel::Serializer | |
self.root = 'users' | |
attributes :id, :name, :email | |
end | |
Benchmark.ips do |benchmark| | |
benchmark.report "Jbuilder (collection)" do | |
Jbuilder.encode do |json| | |
json.users @users do |user| | |
json.(user, :id, :name, :email) | |
end | |
end | |
end | |
benchmark.report "Kartograph (collection)" do | |
UserMapping.represent_collection_for(:read, @users) | |
end | |
benchmark.report "AMS (collection)" do | |
ActiveModel::ArraySerializer.new(@users, root: 'users', each_serializer: UserSerializer).to_json | |
end | |
benchmark.report "Jbuilder (singular)" do | |
Jbuilder.encode do |json| | |
json.user do | |
json.id user.id | |
json.name user.name | |
json.email user.email | |
end | |
end | |
end | |
benchmark.report "Kartograph (singular)" do | |
UserMapping.representation_for(:read, user) | |
end | |
benchmark.report "AMS (singular)" do | |
UserSerializer.new(user, root: 'user').to_json | |
end | |
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
Jbuilder (collection) 249 i/100ms | |
Kartograph (collection) 695 i/100ms | |
AMS (collection) 224 i/100ms | |
Jbuilder (singular) 2390 i/100ms | |
Kartograph (singular) 6502 i/100ms | |
AMS (singular) 2093 i/100ms | |
------------------------------------------------- | |
Jbuilder (collection) 2520.5 (±2.8%) i/s - 12699 in 5.042246s | |
Kartograph (collection) 7126.3 (±3.3%) i/s - 36140 in 5.076994s | |
AMS (collection) 2254.8 (±2.8%) i/s - 11424 in 5.070518s | |
Jbuilder (singular) 25841.8 (±3.8%) i/s - 129060 in 5.001319s | |
Kartograph (singular) 79032.2 (±3.5%) i/s - 396622 in 5.024724s | |
AMS (singular) 23186.9 (±3.4%) i/s - 117208 in 5.060806s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment