Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
#!/usr/bin/env ruby
puts ARGF.read
@jgaskins
jgaskins / benchmark.rb
Created December 20, 2014 14:50
Hashie vs OpenStruct vs PORO performance
require 'hashie'
require 'ostruct'
require 'benchmark/ips'
class PORO
attr_reader :foo, :bar, :baz
def initialize attrs={}
attrs.each do |attr, value|
instance_variable_set "@#{attr}", value
@jgaskins
jgaskins / customer_accounts.rb
Last active August 29, 2015 14:12
Example displaying eager loading with perpetuity-postgres for https://github.com/jgaskins/perpetuity-postgres/issues/18
require 'perpetuity/postgres'
require 'securerandom'
require 'pp'
Perpetuity.data_source 'postgres://localhost/customer_stuff'
module Customer
class User
attr_accessor :name, :email, :api_key, :accounts
@jgaskins
jgaskins / extend_vs_proxy.rb
Last active August 29, 2015 14:14
Object#extend vs proxy object performance
require 'benchmark/ips'
class Foo
def bar
# "Foo#bar"
end
end
module Bar
def bar
@jgaskins
jgaskins / gist:9de193f4babcb56da4ee
Last active August 29, 2015 14:18
Clearwater and the Virtual DOM

I've been experimenting with a lot of things in Clearwater since I first introduced it at B'more on Rails. The reason I showed it to people was because I was hoping to get people talking about front-end development in Ruby with Opal (I've also given a presentation about Opal at B'more on Rails). One of the things that Clearwater does on every link click, at the moment, is rerender the entire app. It does it with a single call, so it's not rendering chunks of HTML here and there, so it's not the worst performance in the world, but I did notice it was clobbering input fields, and micromanaging event handlers (for individually rendered views and attribute-bound blocks) has been a nightmare.

Experiment #1: Components with templates

Components are an interesting way of thinking about web development. Having all of your functionality specified as smaller and smaller subdivisions of content makes perfect sense on th

@jgaskins
jgaskins / 0-result
Created May 18, 2015 17:56
Benchmarking Ruby's Math.sqrt vs raising to the power of 1/2
Calculating -------------------------------------
sqrt 103.322k i/100ms
x ** 0.5 125.661k i/100ms
-------------------------------------------------
sqrt 2.277M (± 6.1%) i/s - 11.365M
x ** 0.5 3.717M (± 5.7%) i/s - 18.598M
Comparison:
x ** 0.5: 3717095.3 i/s
sqrt: 2277003.6 i/s - 1.63x slower
@jgaskins
jgaskins / app_header.rb
Created July 2, 2015 03:12
Example of Clearwater components containing their own styles
class AppHeader
include Clearwater::Component
def render
header({ style: style }, [
h1({ style: app_name_style }, 'My App')
nav({ style: nav_style }, [
NavLink.new('Home', '/'),
NavLink.new('Foo', '/foo'),
NavLink.new('Bar', '/bar'),
@jgaskins
jgaskins / gist:9817fd2c653a0a6ae91f
Last active August 29, 2015 14:24
Ruby proc vs lambda return
def proc_return(enum)
l = proc { |x| return x * 2 }
enum.map { |i| l[i] }
end
def lambda_return(enum)
l = lambda { |x| return x * 2 }
enum.map { |i| l[i] }
@jgaskins
jgaskins / benchmark_method_splat_args.rb
Last active August 29, 2015 14:26
Benchmark methods with splat args in Opal
require 'opal'
class Foo
def opal_no_args_block(*args, &block)
end
%x{
var TMP_3;
def.$js_no_args_block = TMP_3 = function() {
var self = this;
@jgaskins
jgaskins / opal-truthiness-benchmark.rb
Created September 7, 2015 06:29
Opal truthiness benchmark
require 'opal'
def benchmark message, duration=5, &block
measured = 0
count = 0
while measured < duration
start = Time.now
block.call
finish = Time.now
measured += finish - start