$ brew install cloc
$ bundle install
Last active
March 6, 2023 11:15
-
-
Save janko/58e28d42fb268b0ac3c1 to your computer and use it in GitHub Desktop.
ActiveRecord vs Sequel (require time)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "sqlite3" | |
gem "sequel", "4.24" | |
gem "activerecord", "4.2" | |
gem "squeel" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dskecse I'm requiring
squeel
because this is the functionality Sequel offers in addition to Active Record, and it's loaded by default.@alfuken Yeah, loading bare Sequel is not representative. However, loading Sequel plugins like
column_encryption
orsingle_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.I was testing require time plus connecting to the database as well. I'm not sure what else you meant.
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.