Skip to content

Instantly share code, notes, and snippets.

Faster Rails tests

Feedback loop speed in one of the biggest contributing factors to overall development time. The faster you get results, the faster you can move on to other things. A fast enough test suite is therefore critical to teams' success, and is worth investing some time at the beginning to save in the long run.

Below is a list of techniques for speeding up a Rails test suite. It is not comprehensive, but should definitely provide some quick wins. This list of techniques assumes you're using minitest, but most everything should translate over to rspec by simply replacing test/test_helper.rb with spec/spec_helper.rb.

@kddnewton
kddnewton / bignums.rb
Created December 15, 2023 21:39
Bignums
# frozen_string_literal: true
class Number
SIZE = 32
attr_reader :values
def initialize(values = [0])
@values = values
end
@kddnewton
kddnewton / bug.rb
Created November 28, 2023 17:04
Maybe encoding bug?
# frozen_string_literal: true
class Context < BasicObject
def method_missing(name, *) = :identifier
def self.const_missing(name) = :constant
end
$context = Context.new
def evaluate(encoding, codepoint)
@kddnewton
kddnewton / compile.js
Created November 13, 2023 19:07
Compile Prism into YARV in JavaScript
import * as prism from "@ruby/prism";
class RubyInteger {
constructor(value) {
this.value = value;
}
_plus(other) {
if (!(other instanceof RubyInteger)) {
throw new TypeError(`no implicit conversion of ${other.constructor.name} into Integer`);
@kddnewton
kddnewton / json.rb
Last active October 6, 2023 09:25
JSON parser with pattern matching
require "json"
struct = { "a" => 1, "b" => 2, "c" => [1, 2, 3], "d" => [{ "e" => 3 }, nil, false, true, [], {}] }
source = JSON.dump(struct)
tokens = []
index = 0
until source.empty?
tokens <<
@kddnewton
kddnewton / cached.rb
Created September 12, 2023 13:58
Cached lookup
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "~> 7.0.0"
gem "sqlite3"
end
@kddnewton
kddnewton / yarp_ast.rb
Last active August 28, 2023 18:32
Fetch YARP AST for a method
require "yarp"
module YARPAST
def yarp_ast
ast =
begin
RubyVM::AbstractSyntaxTree.of(self, keep_script_lines: true)
rescue ArgumentError
end
@kddnewton
kddnewton / neon.c
Created August 28, 2023 17:50
Attempting to parse identifiers using NEON SIMD
#include <stdbool.h>
#include <stddef.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
// Each entry in this lookup table indicates whether a character is a valid
@kddnewton
kddnewton / json.rb
Last active August 7, 2023 19:54
Faster JSON parser in Ruby
# frozen_string_literal: true
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "json_pure"
gem "benchmark-ips"
end
@kddnewton
kddnewton / assembler.rb
Created March 17, 2023 20:03
Using YARV assembler in Syntax Tree
putspecialobject 3
putnil
defineclass :FooBar, 0
definemethod :foo
putobject "FooBar#foo"
leave
definemethod :bar
putobject "class variable"
setclassvariable :@@bar