Created
May 7, 2020 12:18
-
-
Save casperisfine/0ce1161253ab643f5f15ff63e901b818 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'activerecord' | |
gem 'benchmark-ips' | |
end | |
require 'benchmark/ips' | |
require 'active_record' | |
module ActiveRecord | |
class Base | |
def self.new_dangerous_attribute_method?(name) | |
if self == Base | |
@dangerous_attribute_methods ||= ( | |
instance_methods + | |
private_instance_methods - | |
superclass.instance_methods - | |
superclass.private_instance_methods | |
).map { |m| -m.to_s }.to_set.freeze | |
@dangerous_attribute_methods.include?(name.to_s) | |
else | |
Base.new_dangerous_attribute_method?(name) | |
end | |
end | |
end | |
end | |
class Model < ActiveRecord::Base | |
end | |
Benchmark.ips do |x| | |
x.report('original#hit') { Model.dangerous_attribute_method?('save') } | |
x.report('original#miss') { Model.dangerous_attribute_method?('name') } | |
x.report('new#hit') { Model.new_dangerous_attribute_method?('save') } | |
x.report('new#miss') { Model.new_dangerous_attribute_method?('name') } | |
x.compare! | |
end |
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
Warming up -------------------------------------- | |
original#hit 116.563k i/100ms | |
original#miss 138.858k i/100ms | |
new#hit 228.266k i/100ms | |
new#miss 228.769k i/100ms | |
Calculating ------------------------------------- | |
original#hit 1.571M (± 1.1%) i/s - 7.926M in 5.045043s | |
original#miss 1.953M (± 1.2%) i/s - 9.859M in 5.048572s | |
new#hit 4.211M (± 1.4%) i/s - 21.229M in 5.041961s | |
new#miss 4.256M (± 1.4%) i/s - 21.504M in 5.053145s | |
Comparison: | |
new#miss: 4256484.2 i/s | |
new#hit: 4211316.6 i/s - same-ish: difference falls within error | |
original#miss: 1953077.8 i/s - 2.18x slower | |
original#hit: 1571290.1 i/s - 2.71x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment