Skip to content

Instantly share code, notes, and snippets.

@janko
Last active March 6, 2023 11:15
Show Gist options
  • Save janko/58e28d42fb268b0ac3c1 to your computer and use it in GitHub Desktop.
Save janko/58e28d42fb268b0ac3c1 to your computer and use it in GitHub Desktop.
ActiveRecord vs Sequel (require time)
$ brew install cloc
$ bundle install
require "yaml"
def lib_dir(gem_name)
gem = Gem::Specification.find_by_name(gem_name)
File.join(gem.full_gem_path, gem.require_path)
end
def loc(gem_name)
output = `cloc #{lib_dir(gem_name)} --yaml`
yaml = output.lines[6..-1].join("\n")
YAML.load(yaml)["Ruby"]["code"]
end
sequel_loc = loc("sequel")
activerecord_loc = loc("activerecord") +
loc("arel") +
loc("activemodel") +
loc("squeel")
puts "Sequel LOC: #{sequel_loc}"
puts "ActiveRecord LOC: #{activerecord_loc}"
# Sequel LOC: 31277
# ActiveRecord LOC: 27384
require "benchmark"
require "sqlite3"
def require_sequel
require "sequel"
Sequel.sqlite
end
def require_activerecord
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
files("activerecord").grep(/association/).each &method(:require)
require "squeel"
end
def files(gem_name)
gem = Gem::Specification.find_by_name(gem_name)
lib_dir = File.join(gem.full_gem_path, gem.require_path)
files = Dir["#{lib_dir}/**/*.rb"]
end
puts "Sequel load time: #{Benchmark.realtime{require_sequel}}"
puts "ActiveRecord load time: #{Benchmark.realtime{require_activerecord}}"
# Sequel load time: 0.08
# ActiveRecord load time: 0.4 (5 times slower)
source "https://rubygems.org"
gem "sqlite3"
gem "sequel", "4.24"
gem "activerecord", "4.2"
gem "squeel"
@janko
Copy link
Author

janko commented Mar 6, 2023

Why do you require squeel inside the require_activerecord method? If you're benchmarking the require time of both gems I'd assume you only require them.

@dskecse I'm requiring squeel because this is the functionality Sequel offers in addition to Active Record, and it's loaded by default.

Not only that, but you also comparing the loading of full suite of AR functionality vs basic bare Sequel.

@alfuken Yeah, loading bare Sequel is not representative. However, loading Sequel plugins like column_encryption or single_table_inheritance wouldn't be fair either, because with Sequel you don't have to load code for features you're not using, unlike with Active Record where you have to load everything.

Second, comparing things by simply requiring is half the deal, you should be comparing full code initialisation time, since that is an integral part as well.

I was testing require time plus connecting to the database as well. I'm not sure what else you meant.

And, lastly, advocating a library based on a difference in 1/3 of a second boot time, is like saying "you should choose Audi over BMW because it's engine starts in 0.3s faster". This says more about the person comparing them, rather than about compared objects, honestly.

I wasn't claiming that Sequel is superior because you start with lower boot time, it's just one of multiple factors. For me boot time plays a role in developer happiness, so it's not just a theoretical benchmark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment