Skip to content

Instantly share code, notes, and snippets.

@serradura
Created March 26, 2021 14:59
Show Gist options
  • Save serradura/896ac4a8f09fc97de47057fc0bb98447 to your computer and use it in GitHub Desktop.
Save serradura/896ac4a8f09fc97de47057fc0bb98447 to your computer and use it in GitHub Desktop.
Benchmark of basic strict type checking - dry-types VS kind
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips', '~> 2.8', '>= 2.8.4'
gem 'dry-types', '~> 1.5', '>= 1.5.1'
gem 'kind', '~> 5.4'
end
require 'kind'
require 'dry-types'
module Types
include Dry.Types()
end
STR = ''.freeze
require 'benchmark/ips'
# require 'kind/strict/disabled'
Benchmark.ips do |x|
x.report('dry-types') { Types::Strict::String[STR] }
x.report('kind object') { Kind::String[STR] }
x.report('kind dynamic checking') { Kind.of(String, STR) }
x.compare!
end
# =============================================
# Result using `require "kind/strict/disabled"`
# =============================================
# Warming up --------------------------------------
# dry-types 304.522k i/100ms
# kind object 731.471k i/100ms
# kind dynamic checking 732.261k i/100ms
# Calculating -------------------------------------
# dry-types 3.108M (± 1.3%) i/s - 15.835M in 5.095952s
# kind object 7.339M (± 1.4%) i/s - 37.305M in 5.084224s
# kind dynamic checking 7.280M (± 2.2%) i/s - 36.613M in 5.031921s
# Comparison:
# kind object: 7338937.4 i/s
# kind dynamic checking: 7280016.1 i/s - same-ish: difference falls within error
# dry-types: 3107904.0 i/s - 2.36x (± 0.00) slower
# ===============================================
# Result without `require "kind/strict/disabled"`
# ===============================================
# Warming up --------------------------------------
# dry-types 320.100k i/100ms
# kind object 527.765k i/100ms
# kind dynamic checking 653.915k i/100ms
#
# Calculating -------------------------------------
# dry-types 3.269M (± 1.2%) i/s - 16.645M in 5.092458s
# kind object 5.223M (± 1.4%) i/s - 26.388M in 5.053669s
# kind dynamic checking 6.378M (± 1.4%) i/s - 32.042M in 5.024411s
# Comparison:
# kind dynamic checking: 6378458.9 i/s
# kind object: 5222670.1 i/s - 1.22x (± 0.00) slower
# dry-types: 3269064.8 i/s - 1.95x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment