Skip to content

Instantly share code, notes, and snippets.


Jamie Gaskins jgaskins

View GitHub Profile
jgaskins /
Last active May 28, 2021
Benchmarking Redis drivers: Ruby gem, Hiredis, and a custom Ruby implementation


Name Description
ruby The redis gem with the plain-Ruby driver
hiredis The redis gem with the hiredis driver (compiled as a C extension)
myredis Pure-Ruby, optimized for the specific operation


jgaskins / benchmark.rb
Last active Apr 30, 2021
Benchmarking redis gem vs hiredis vs custom Redis client
View benchmark.rb
require 'bundler/inline'
require 'socket'
class MyRedis
CRLF = "\r\n"
def initialize
@connection ='localhost', 6379)
@connection.sync = false
jgaskins /
Created Jan 18, 2021
Roda-like routing mixin for Crystal
require "http"
require "./route"
class App
include HTTP::Handler
include Route
def call(context)
route context do |r, response|
jgaskins /
Created Dec 3, 2020
Benchmarking allocation time of Crystal objects with various quantities of instance variables
require "benchmark"
value = nil
Benchmark.ips do |x| "1 ivar" { value = } "2 ivars" { value = } "4 ivars" { value = } "8 ivars" { value = } "16 ivars" { value = }
jgaskins /
Created Jun 28, 2020
Compare parsing an int as intermediate strings vs using a byte parser directly on the IO
require "benchmark"
int = 0
[1, 12, 1234, 12345678, 1234567812345678].each do |number|
puts "Parsing #{number}..."
io =
io << ":#{number}\r\n" # Redis integer format
Benchmark.ips do |x|
jgaskins /
Created May 28, 2020
Simple Redis implementation in Crystal
require "socket"
module Redis
alias Type = String | Int64 | Nil | Array(Type)
class Future
property! value : Type
module Commands
jgaskins /
Created May 28, 2020
Adding stream support to Crystal Redis
class Redis
module Commands
def xadd(stream, id, params : Hash)
params = params.each_with_object(Array(String).new(params.size * 2)) do |(key, value), array|
array << key << value.to_s
string_command(["XADD", stream, id] + params)
def xrange(stream, start, finish)
jgaskins / event_handlers.js
Created Jan 31, 2020
Managing your own DOM event handlers in JS
View event_handlers.js
/* Data structure to hold our event handlers. It's important that when you
remove an element from the DOM that you delete its associated event
handler id to avoid memory leaks.
12345: { // DOM element's event handler id
click: [
function(event) { ... },
jgaskins / fetch.cypher
Created Nov 17, 2019
Fetching a remote user
View fetch.cypher
// The $url is passed in as the only parameter to this query. The query
// handles everything else.
CALL apoc.load.jsonParams($url, {Accept: 'application/json'}, null, null, {}) YIELD value AS data
// Upsert the person into the DB, using the :Person label in case they aren't
// a fully-fledged account yet
MERGE (person:Person { id: })
person.created_at = datetime()
jgaskins / set_operations.rb
Created Oct 13, 2019
SQL result set operations for ActiveRecord
View set_operations.rb
module SetOperations
def union_scope(*scopes)
apply_operation :UNION, scopes
def intersect_scope(*scopes)
apply_operation :INTERSECT, scopes
def except_scope(*scopes)