Skip to content

Instantly share code, notes, and snippets.

View jodosha's full-sized avatar

Luca Guidi jodosha

View GitHub Profile
@jodosha
jodosha / benchmark.rb
Created July 22, 2020 07:37
Ruby Method Overloading
require "benchmark/ips"
require_relative "./method_overloading"
class Foo
include MethodOverloading
def call(number)
"foo #{number}"
end
end
@jodosha
jodosha / thread_current.rb
Created July 9, 2020 09:04
Limits of Ruby's Thread.current
# frozen_string_literal: true
Thread.current[:request_id] = 'abc123'
puts "main thread: #{Thread.current[:request_id]}"
Thread.new do
puts "inner thread: #{Thread.current[:request_id]}"
end.join
@jodosha
jodosha / memory.rb
Created September 16, 2019 08:45
Ruby speed & memory bench: Array#compact vs nil check
# frozen_string_literal: true
require "pathname"
require "memory_profiler"
class Env
def load!(*)
end
end
@jodosha
jodosha / bench.rb
Last active October 13, 2022 05:15
Ruby benchmark: Array#uniq vs Set#to_a
#!/usr/bin/env ruby
require "benchmark/ips"
require "set"
INPUT = 100.times.map { "my-test-string" }.freeze
Benchmark.ips do |x|
x.report("Array#uniq") { INPUT.uniq }
x.report("Set#to_a") { Set.new(INPUT).to_a }
# frozen_string_literal: true
source "https://rubygems.org"
gem "rack"
gem "puma"
gem "guard-puma"
@jodosha
jodosha / Gemfile
Created January 7, 2019 08:47
dry-view ERB capture syntax example
# frozen_string_literal: true
source "https://rubygems.org"
gem "dry-view", git: "https://github.com/dry-rb/dry-view.git", branch: "erb-support"
gem "erbse"
@jodosha
jodosha / discard.rb
Created December 13, 2018 14:23
Ruby parallel assignment benchmark
#!/usr/bin/env ruby
# frozen_string_literal: true
require "benchmark/ips"
def validate
[false, "not valid"]
end
Benchmark.ips do |x|
@jodosha
jodosha / bench.rb
Created December 18, 2017 09:29
Ruby string interpolation bench: annotated vs unannotated (http://rubocop.readthedocs.io/en/latest/cops_style/#styleformatstringtoken)
#!/usr/bin/env ruby
require 'benchmark/ips'
require 'bundler/setup'
require 'dnsimple'
account_id = 1010
zone_id = 100
record_id = 999
Benchmark.ips do |x|
@jodosha
jodosha / request_id.rb
Created December 4, 2017 20:17
Ruby benchmark `SecureRandom.hex(16)` vs `SecureRandom.uuid`
#!/usr/bin/env ruby
# frozen_string_literal: true
require "benchmark/ips"
require "securerandom"
Benchmark.ips do |x|
x.report("hex(16)") { SecureRandom.hex(16) }
x.report("uuid") { SecureRandom.uuid }
x.compare!
@jodosha
jodosha / bench.rb
Created November 17, 2017 18:55
Ruby: Hash#key? vs Set#include?
#!/usr/bin/env ruby
require 'benchmark/ips'
require 'set'
SET = (4..16).to_set.freeze
HASH = SET.each_with_object({}) { |n, ret| ret[n] = true }.freeze
Benchmark.ips do |x|
x.report('set') { SET.include?(16) }
x.report('hash') { HASH.key?(16) }