Last active
June 5, 2019 23:43
-
-
Save sofakingworld/0d82d0f0f5d4dbec56e2e6fd5fbfbf54 to your computer and use it in GitHub Desktop.
Custom serializer benchmarking
This file contains hidden or 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
user = User.first | |
require 'Benchmark' | |
Benchmark.bm do |x| | |
x.report { 1000.times do UserSerializer.serialize(user) end} | |
x.report { 1000.times do FJUserSerializer.new(user).serializable_hash end} | |
end | |
# # => | |
# user system total real | |
# 0.000697 0.000175 0.000872 ( 0.000869) | |
# 0.014885 0.001507 0.016392 ( 0.016488) |
This file contains hidden or 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
class Serializer | |
def self.serialize(object) | |
methods(false).map do |function_name| | |
[function_name, self.send(function_name, object)] | |
end.to_h | |
end | |
end | |
class UserSerializer < Serializer | |
module_function | |
def self.email(object) | |
object.email | |
end | |
def self.id(object) | |
object.id | |
end | |
def self.uid(object) | |
object.uid | |
end | |
end |
This file contains hidden or 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
# Gem https://github.com/Netflix/fast_jsonapi | |
class FJUserSerializer | |
include FastJsonapi::ObjectSerializer | |
attributes :id, :email, :uid | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment