Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / ar_vs_perpetuity.markdown
Last active January 1, 2016 17:38
Benchmarking ActiveRecord vs Perpetuity::Postgres

Benchmarking ActiveRecord vs Perpetuity::Postgres

Running through a set of common CRUD operations. Each object on both ORMs uses a single model containing 3 attributes: a String, a Fixnum, and a Time. Each benchmark operates on 5000 objects.

Instantiating 5000 objects

           user     system      total        real
AR     0.230000   0.010000   0.240000 (  0.240430)
PORO 0.000000 0.000000 0.000000 ( 0.001539)
@jgaskins
jgaskins / numeric_vs_uuid
Created January 17, 2014 01:38
Comparing numeric vs UUID in Postgres. Rehearsal benchmarks are thrown out. Only the second run is kept. Insertions are done all in one query to minimize the impact of I/O. This is harder to do with retrieval, but the ids are randomized in each one to minimize the effects of caching inside the database.
Inserting
user system total real
Numeric 0.070000 0.010000 0.080000 ( 0.586738)
UUID 0.070000 0.020000 0.090000 ( 3.101085)
Retrieving by id
user system total real
Numeric 0.810000 0.980000 1.790000 ( 6.831551)
UUID 0.830000 0.990000 1.820000 ( 6.981944)
@jgaskins
jgaskins / 0-results
Created February 23, 2014 04:39
Benchmarking Nokogiri, LibXML, and Ox
Parsing...
Calculating -------------------------------------
nokogiri 127 i/100ms
libxml 130 i/100ms
ox 716 i/100ms
-------------------------------------------------
nokogiri 1334.7 (±15.1%) i/s - 6604 in 5.215835s
libxml 1287.5 (±1.9%) i/s - 6500 in 5.050367s
ox 7228.3 (±3.7%) i/s - 36516 in 5.059256s
@jgaskins
jgaskins / application.rb
Last active August 29, 2015 13:57
Charging customers during registration with Devise
module Foo
class Application < Rails::Application
# Keep the credit card data out of your logs
config.filter_parameters += [:password, :credit_card]
end
end
@jgaskins
jgaskins / ar_model.rb
Created March 28, 2014 01:19
ActiveRecord default attributes
require 'active_record'
require 'pg'
ActiveRecord::Base.establish_connection host: 'localhost',
database: 'jamie',
adapter: 'postgresql'
class ARModel < ActiveRecord::Base
def initialize(attributes={})
# AR::Base#create w/o args passes nil because lol.
@jgaskins
jgaskins / Gemfile
Created April 23, 2014 13:27
Backporting Rails 4 caching to Rails 3.2
# Add these two gems
gem 'thread_safe'
gem 'cache_digests'
@jgaskins
jgaskins / fib.rb
Created June 5, 2014 00:56
Fibonacci Benchmark: Swift vs Rubinius vs JRuby vs MRI
require 'benchmark'
def fib n
if n <= 1
n
else
fib(n - 1) + fib(n - 2)
end
end
@jgaskins
jgaskins / foo.rb
Created June 13, 2014 20:15
Short-circuiting OR/AND in Ruby, represented as methods
# If it were possible to override && and || in Ruby, this is how it might look.
class NilClass
def && other
self
end
def || other
other
end
@jgaskins
jgaskins / foo.coffee
Last active August 29, 2015 14:04
Default values
class Foo
def initialize: (attributes={}) ->
# Set default day to Monday
# This broke for Sunday (attributes.day == 0)
@day = attributes.day || 1
# How I ended up doing it
@day = if attributes.hasOwnProperty('day') then attributes.day else 1
@jgaskins
jgaskins / articles_controller.rb
Created August 19, 2014 03:37
Controllers with AR vs Perpetuity
class ArticlesController < ApplicationController
# with ActiveRecord
def create
@article = Article.new(params[:article])
if @article.save
redirect_to @article, notice: 'Article created!'
else
render :new
end