Skip to content

Instantly share code, notes, and snippets.

@localhostdotdev
Last active March 18, 2019 12:54
Show Gist options
  • Select an option

  • Save localhostdotdev/c4a86a1e6e59f5f3e042887f1fdea755 to your computer and use it in GitHub Desktop.

Select an option

Save localhostdotdev/c4a86a1e6e59f5f3e042887f1fdea755 to your computer and use it in GitHub Desktop.
Rails ActiveRecord#select vs #pluck, 5x faster (~20k records, 10 runs each)
class Utils
def self.compare(reports, n: 10)
Benchmark.bm do |x|
reports.each do |report|
if report.is_a?(Proc)
x.report { n.times { report.call } }
else
x.report(report.first) { n.times { report.last.call } }
end
end
end
end
end
Utils.compare([['select', -> { Blob.select(:id).to_a }], ['pluck', -> { Blob.pluck(:id) }]], n: 10)
user system total real
select 1.296538 0.021889 1.318427 ( 1.460752)
pluck 0.184873 0.011837 0.196710 ( 0.334181)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment