Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created April 24, 2019 08:59
Show Gist options
  • Save kamipo/e427f080a27b46f50bc508fae3612a0e to your computer and use it in GitHub Desktop.
Save kamipo/e427f080a27b46f50bc508fae3612a0e to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
gem "sqlite3"
gem "benchmark-ips"
end
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 :users, force: true do |t|
t.string :name
t.timestamps null: false
end
end
class User < ActiveRecord::Base
alias_attribute :new_id, :id
alias_attribute :new_name, :name
end
user = User.create!(name: "user name")
Benchmark.ips do |x|
x.report("user['id']") { user['id'] }
x.report("user['new_id']") { user['new_id'] }
x.report("user['name']") { user['name'] }
x.report("user['new_name']") { user['new_name'] }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment