Skip to content

Instantly share code, notes, and snippets.

View davydovanton's full-sized avatar
:shipit:
Working from home

Anton Davydov davydovanton

:shipit:
Working from home
View GitHub Profile
require 'thor'
module Hanami
module Auth
def Auth.included(thor)
thor.class_eval do
desc 'custom_command', 'Desc'
long_desc <<-EOS
EOS
def auth
require 'benchmark/ips'
FRIZED_HASH = {
a: 'a',
b: 'b',
c: 'c',
d: 'd',
e: 'e'
}.freeze
@davydovanton
davydovanton / each_with_object_vs_map.rb
Last active November 11, 2018 13:33
Benchmark: each_with_object vs map!
require 'benchmark/ips'
array = (1..10_000).to_a
Benchmark.ips do |x|
x.report('each_with_object') { array.each_with_object([]) { |i,a| a << (i * 2) } }
x.report('map') { array.map { |i| i * 2 } }
x.report('map!') { array.map! { |i| i * 2 } }
x.report('reduse') { array.reduce([]) { |a,i| a << i * 2 } }
x.compare!
@davydovanton
davydovanton / shallow_attributes_vs_virtus.rb
Last active February 7, 2019 16:45
Benchmark: shallow_attributes vs virtus
require 'benchmark/ips'
require 'shallow_attributes'
require 'virtus'
hash = {
addresses: [
{
street: 'Street 1/2',
city: {
name: 'NYC'
@davydovanton
davydovanton / hash_to_a_vs_first.rb
Created March 10, 2016 18:59
Benchmark: hash first, to_a.first vs keys.first
require 'benchmark/ips'
hash = {a: 1, b: 2, c: 3, d: 4}
Benchmark.ips do |x|
x.report('first') { hash.first }
x.report('to_a.first') { hash.to_a.first }
x.report('keys.first') do
key = hash.keys.first
[key, hash[key]]
@davydovanton
davydovanton / inject_vs_each.rb
Created March 7, 2016 14:17
Benchmark: inject vs each
require 'benchmark/ips'
numbers = (1..25).to_a
Benchmark.ips do |x|
x.report('each') do
sum = 0
numbers.each do |number|
sum += number
end
@davydovanton
davydovanton / multiple_assignments.rb
Created February 14, 2016 12:23
Benchmark: get first array element
require 'benchmark/ips'
array = [1, 2]
Benchmark.ips do |x|
x.report('multiple assignments'){ a, _ = array }
x.report(' #first method call'){ a = array.first }
x.report(' #[] method call'){ a = array[0] }
x.compare!
end
@davydovanton
davydovanton / split_vs_slice.rb
Last active February 13, 2016 08:35
Benchmark: split vs slice
require 'benchmark/ips'
str = 'hello_world_this_is_bench_for_slice_and_split'
Benchmark.ips do |x|
x.report('split'){ str.split('_').first }
x.report('slice'){ str.slice(/[^_]+/, 0) }
x.compare!
end
@davydovanton
davydovanton / bench.rb
Created January 11, 2016 23:16
Benchmark for #handle_exception in lotus
require 'lotus-controller'
require 'benchmark/ips'
class MyCustomError < StandardError; end
class MySecondCustomError < MyCustomError; end
def first_method(exception)
@handled_exceptions.merge!(exception)
@handled_exceptions = Hash[
@handled_exceptions.sort{|(ex1,_),(ex2,_)| ex1.ancestors.include?(ex2) ? -1 : 1 }
@davydovanton
davydovanton / bench.rb
Last active January 5, 2016 17:29
String bench
require 'benchmark/ips'
foo = 'foo'
bar = 'bar'
Benchmark.ips do |x|
x.report('+ and #to_s') { foo.to_s + bar.to_s }
x.report('+') { foo + bar }
x.report('#{}') { "#{foo}#{bar}" }
x.compare!
end