Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created February 25, 2020 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamipo/296b9dd97f690d7960fdfe8de3ca08c7 to your computer and use it in GitHub Desktop.
Save kamipo/296b9dd97f690d7960fdfe8de3ca08c7 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 "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
t.string :author
t.boolean :published
t.integer :score
t.string :tags
t.text :content
t.integer :another_field
end
end
class Post < ActiveRecord::Base
end
SCENARIOS = {
"All symbols" => {
title: "My post",
author: "John",
published: true,
score: 500,
tags: "Software development,Web development",
content: "random text" * 100,
another_field: 55555
},
"Mixed" => {
"title" => "My post",
author: "John",
"published" => true,
score: 500,
"tags" => "Software development,Web development",
content: "random text" * 100,
"another_field" => 55555
},
"All strings" => {
"title" => "My post",
"author" => "John",
"published" => true,
"score" => 500,
"tags" => "Software development,Web development",
"content" => "random text" * 100,
"another_field" => 55555
}
}
SCENARIOS.each_pair do |name, value|
puts
puts " #{name} ".center(80, "=")
puts
post = Post.new
Benchmark.ips do |x|
x.report("assign_attributes") { post.assign_attributes(value) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment