Last active
December 11, 2017 23:33
-
-
Save solnic/9887ba2549bba43a890f to your computer and use it in GitHub Desktop.
Anima + Transproc vs Virtus
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 'anima' | |
require 'transproc' | |
require 'virtus' | |
require 'benchmark/ips' | |
USERS = 1000.times.map { |i| { id: "#{i+1}", name: "User #{i+1}", age: "#{(i+1)*2}" } } | |
module Mappings | |
extend Transproc::Registry | |
import :accept_keys, from: Transproc::HashTransformations | |
import :map_value, from: Transproc::HashTransformations | |
import :to_integer, from: Transproc::Coercions | |
end | |
class AnimaUser | |
include Anima.new(:id, :name, :age) | |
end | |
class VirtusUser | |
include Virtus.value_object | |
values do | |
attribute :id, Integer | |
attribute :name, String | |
attribute :age, Integer | |
end | |
end | |
build_virtus_user = -> input { VirtusUser.new(input) } | |
map_user_input = Mappings[:accept_keys, [:id, :name, :age]] | |
.>> Mappings[:map_value, :id, Mappings[:to_integer]] | |
.>> Mappings[:map_value, :age, Mappings[:to_integer]] | |
build_anima_user = -> input { AnimaUser.new(map_user_input[input]) } | |
Benchmark.ips do |x| | |
x.report "anima" do | |
USERS.each { |attrs| build_anima_user[attrs] } | |
end | |
x.report "virtus" do | |
USERS.each { |attrs| build_virtus_user[attrs] } | |
end | |
x.compare! | |
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
Calculating ------------------------------------- | |
anima 8.000 i/100ms | |
virtus 1.000 i/100ms | |
------------------------------------------------- | |
anima 83.732 (± 4.8%) i/s - 424.000 | |
virtus 6.409 (± 0.0%) i/s - 32.000 | |
Comparison: | |
anima: 83.7 i/s | |
virtus: 6.4 i/s - 13.06x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment