Sha256: 463300877c9210101444adc850d62b842b6cbca79e33de65f0b02dc0b81526e9

Contents?: true

Size: 1007 Bytes

Versions: 17

Compression:

Stored size: 1007 Bytes

Contents

require "rubygems"
require "bundler/setup"
require "active_model_serializers"
require "active_support/json"
require "benchmark"

class User < Struct.new(:id,:name,:age,:about) 
  include ActiveModel::SerializerSupport
  
  def fast_hash
    h = { 
      id: read_attribute_for_serialization(:id), 
      name: read_attribute_for_serialization(:name), 
      about: read_attribute_for_serialization(:about)
    }
    h[:age] = read_attribute_for_serialization(:age) if age > 18
    h
  end
end

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :age, :about

  def include_age?
    object.age > 18
  end
end



u = User.new(1, "sam", 10, "about")
s = UserSerializer.new(u)

n = 100000

Benchmark.bmbm {|x| 
  x.report("init") { n.times { UserSerializer.new(u) } }
  x.report("fast_hash") { n.times { u.fast_hash } }
  x.report("attributes") { n.times { UserSerializer.new(u).attributes } } 
  x.report("serializable_hash") { n.times { UserSerializer.new(u).serializable_hash } }
}   


Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
active_model_serializers-0.8.4 bench/perf.rb
active_model_serializers-0.8.3 bench/perf.rb
active_model_serializers-0.8.2 bench/perf.rb
gpi-active_model_serializers-0.8.5 bench/perf.rb
gpi-active_model_serializers-0.8.5.alpha1 bench/perf.rb
gpi-active_model_serializers-0.8.4 bench/perf.rb
gpi-active_model_serializers-0.8.4.alpha5 bench/perf.rb
gpi-active_model_serializers-0.8.4.alpha4 bench/perf.rb
gpi-active_model_serializers-0.8.4.alpha3 bench/perf.rb
gpi-active_model_serializers-0.8.4.alpha2 bench/perf.rb
gpi-active_model_serializers-0.8.4.alpha1 bench/perf.rb
gpi-active_model_serializers-0.8.3.rc1 bench/perf.rb
gpi-active_model_serializers-0.8.3.alpha2 bench/perf.rb
gpi-active_model_serializers-0.8.3.alpha1 bench/perf.rb
gpi-active_model_serializers-0.8.2 bench/perf.rb
active_model_serializers-0.8.1 bench/perf.rb
active_model_serializers-0.8.0 bench/perf.rb