Skip to content

Instantly share code, notes, and snippets.

@kapkaev
Created February 13, 2013 08:38
Show Gist options
  • Save kapkaev/4943119 to your computer and use it in GitHub Desktop.
Save kapkaev/4943119 to your computer and use it in GitHub Desktop.
oj vs activemodel_serializer
require 'benchmark'
require 'oj'
class WrapperSerializer < ActiveModel::Serializer
attributes :seq
end
class Wrapper
include ActiveModel::SerializerSupport
def initialize
@seq = []
100.times {@seq << A.new}
end
attr_accessor :seq
end
class A
include ActiveModel::SerializerSupport
def initialize
@x = 1
@y = 2
@z = B.new
end
attr_accessor :x, :y, :z
end
class B
include ActiveModel::SerializerSupport
def initialize
@x = 1
@y = 2
@z = C.new
end
attr_accessor :x, :y, :z
end
class C
include ActiveModel::SerializerSupport
def initialize
@x = 1
@y = 2
end
attr_accessor :x, :y, :z
end
Benchmark.realtime { Oj.dump(Wrapper.new ) }
Benchmark.realtime { Oj.dump(Wrapper.new, {:mode => :compat}) }
Benchmark.realtime { WrapperSerializer.new(Wrapper.new).as_json}
Benchmark.realtime { WrapperSerializer.new(Wrapper.new).to_json}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment