
Benchmark
require 'benchmark/ips'
Benchmark.ips do |x|
LIMIT = 20000
columns = %i(id)
x.report('as_json') { Incident.limit(LIMIT).select(*columns).as_json }
x.report('pluck_json') { Incident.limit(LIMIT).pluck_json(*columns) }
x.compare!
end
Calculating -------------------------------------
as_json 1.000 i/100ms
pluck_json 1.000 i/100ms
-------------------------------------------------
as_json 0.585 (± 0.0%) i/s - 4.000 in 6.876732s
pluck_json 16.215 (±12.3%) i/s - 80.000
Comparison:
pluck_json: 16.2 i/s
as_json: 0.6 i/s - 27.72x slower
Benchmark.ips do |x|
LIMIT = 20000
columns = %i(id category_id user_id)
x.report('as_json') { Incident.limit(LIMIT).select(*columns).as_json }
x.report('pluck_json') { Incident.limit(LIMIT).pluck_json(*columns) }
x.compare!
end
Calculating -------------------------------------
as_json 1.000 i/100ms
pluck_json 1.000 i/100ms
-------------------------------------------------
as_json 0.550 (± 0.0%) i/s - 3.000 in 5.463522s
pluck_json 10.586 (± 9.4%) i/s - 53.000
Comparison:
pluck_json: 10.6 i/s
as_json: 0.5 i/s - 19.25x slower
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/pavlik4k/pluck_json. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.