Skip to content

Instantly share code, notes, and snippets.

@alexcameron89
Created August 10, 2016 14:04
Show Gist options
  • Save alexcameron89/2eb645e6d8bea28f8b909ebf287a81fa to your computer and use it in GitHub Desktop.
Save alexcameron89/2eb645e6d8bea28f8b909ebf287a81fa to your computer and use it in GitHub Desktop.
Benchmark Active Record #find(id) vs. #where(id: id)
require 'benchmark/ips'
require 'active_record'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :projects
end
class Project < ActiveRecord::Base
end
id = Project.create.id
Benchmark.ips do |x|
x.report('find') { Project.find(id) }
x.report('where id') { Project.where(id: id) }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment