Skip to content

Instantly share code, notes, and snippets.

Avatar

Luca Guidi jodosha

View GitHub Profile
@jodosha
jodosha / benchmark.rb
Created Jul 22, 2020
Ruby Method Overloading
View benchmark.rb
require "benchmark/ips"
require_relative "./method_overloading"
class Foo
include MethodOverloading
def call(number)
"foo #{number}"
end
end
@jodosha
jodosha / thread_current.rb
Created Jul 9, 2020
Limits of Ruby's Thread.current
View thread_current.rb
# 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 Sep 16, 2019
Ruby speed & memory bench: Array#compact vs nil check
View memory.rb
# frozen_string_literal: true
require "pathname"
require "memory_profiler"
class Env
def load!(*)
end
end
@jodosha
jodosha / bench.rb
Last active Aug 8, 2019
Ruby benchmark: Array#uniq vs Set#to_a
View bench.rb
#!/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 }
View Gemfile
# frozen_string_literal: true
source "https://rubygems.org"
gem "rack"
gem "puma"
gem "guard-puma"
@jodosha
jodosha / Gemfile
Created Jan 7, 2019
dry-view ERB capture syntax example
View Gemfile
# 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 Dec 13, 2018
Ruby parallel assignment benchmark
View discard.rb
#!/usr/bin/env ruby
# frozen_string_literal: true
require "benchmark/ips"
def validate
[false, "not valid"]
end
Benchmark.ips do |x|
View bench.rb
#!/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 Dec 4, 2017
Ruby benchmark `SecureRandom.hex(16)` vs `SecureRandom.uuid`
View request_id.rb
#!/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 Nov 17, 2017
Ruby: Hash#key? vs Set#include?
View bench.rb
#!/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) }