Skip to content

Instantly share code, notes, and snippets.

@yuki24
Last active April 16, 2016 20:05
Show Gist options
  • Save yuki24/4604675 to your computer and use it in GitHub Desktop.
Save yuki24/4604675 to your computer and use it in GitHub Desktop.
Jbuilder is faster than Ralb.
require 'benchmark/ips'
require 'active_support/core_ext'
require 'jbuilder'
require 'rabl'
require 'oj'
Benchmark.ips do |benchmark|
struct = Struct.new(:name, :birthyear, :bio, :url)
@author = struct.new("Rolf", 1920, "Software developer", "http://example.com/")
@authors = Array.new(10, @author)
# for jbuilder
@author.instance_eval { undef map }
# for rabl
@scope = Object.new
@scope.instance_variable_set :@author, @author
@template_for_single_object = %{
object @author => :author
attribute :name, :birthyear, :bio, :url
}
@scope.instance_variable_set :@authors, @authors
@template_for_collection = %{
collection @authors => :authors
attributes :name, :birthyear, :bio, :url
}
benchmark.report "Jbuilder(single obj)" do
Jbuilder.encode do |json|
json.author @author, :name, :birthyear, :bio, :url
end
end
benchmark.report "Rabl(single obj)" do
Rabl::Engine.new(@template_for_single_object).render(@scope, {})
end
benchmark.report "Jbuilder(collection)" do
Jbuilder.encode do |json|
json.authors @authors do |author|
json.author author, :name, :birthyear, :bio, :url
end
end
end
benchmark.report "Rabl(collection)" do
Rabl::Engine.new(@template_for_collection).render(@scope, {})
end
end
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'activesupport'
gem 'jbuilder'
gem 'rabl'
gem 'oj'
GEM
remote: https://rubygems.org/
specs:
activesupport (4.0.2)
i18n (~> 0.6, >= 0.6.4)
minitest (~> 4.2)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
atomic (1.1.14)
benchmark-ips (1.2.0)
i18n (0.6.9)
jbuilder (2.0.2)
activesupport (>= 3.0.0)
multi_json (>= 1.2.0)
minitest (4.7.5)
multi_json (1.8.4)
oj (2.5.4)
rabl (0.9.3)
activesupport (>= 2.3.14)
thread_safe (0.1.3)
atomic
tzinfo (0.3.38)
PLATFORMS
ruby
DEPENDENCIES
activesupport
benchmark-ips
jbuilder
oj
rabl
@yuki24
Copy link
Author

yuki24 commented Jan 23, 2013

Result(from the revesion d0696b8d):

                                user     system      total        real
Jbuilder(single object)     1.040000   0.000000   1.040000 (  1.045209)
Rabl(single object)         2.350000   0.000000   2.350000 (  2.368760)
Jbuilder(collection)        2.780000   0.000000   2.780000 (  2.798979)
Rabl(collection)           12.910000   0.020000  12.930000 ( 12.972485)

@yuki24
Copy link
Author

yuki24 commented Feb 3, 2014

updated the code/gems and ran the above again(higher is better):

$ bundle exec ruby benchmark.rb 
Calculating -------------------------------------
Jbuilder(single obj)      2932 i/100ms
    Rabl(single obj)       870 i/100ms
Jbuilder(collection)       984 i/100ms
    Rabl(collection)       273 i/100ms
-------------------------------------------------
Jbuilder(single obj)    42435.5 (±5.0%) i/s -     214036 in   5.058724s
    Rabl(single obj)    10409.0 (±5.7%) i/s -      52200 in   5.031122s
Jbuilder(collection)    11913.3 (±4.0%) i/s -      60024 in   5.046583s
    Rabl(collection)     2817.2 (±10.2%) i/s -      13923 in   5.017393s

Ruby version:

$ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]

jbuilder is almost 4 times faster than rabl.

@constantm
Copy link

I wonder if these benchmarks still hold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment