discourse/script/micro_bench.rb
Ted Johansson 25a226279a
DEV: Replace #pluck_first freedom patch with AR #pick in core (#19893)
The #pluck_first freedom patch, first introduced by @danielwaterworth has served us well, and is used widely throughout both core and plugins. It seems to have been a common enough use case that Rails 6 introduced it's own method #pick with the exact same implementation. This allows us to retire the freedom patch and switch over to the built-in ActiveRecord method.

There is no replacement for #pluck_first!, but a quick search shows we are using this in a very limited capacity, and in some cases incorrectly (by assuming a nil return rather than an exception), which can quite easily be replaced with #pick plus some extra handling.
2023-02-13 12:39:45 +08:00

21 lines
570 B
Ruby

# frozen_string_literal: true
require "benchmark/ips"
require File.expand_path("../../config/environment", __FILE__)
conn = ActiveRecord::Base.connection.raw_connection
Benchmark.ips do |b|
b.report("simple") { User.first.name }
b.report("simple with select") { User.select("name").first.name }
b.report("pluck with first") { User.pluck(:name).first }
b.report("pluck with limit") { User.limit(1).pluck(:name).first }
b.report("pluck with pick") { User.pick(:name) }
b.report("raw") { conn.exec("SELECT name FROM users LIMIT 1").getvalue(0, 0) }
end