Skip to content

Instantly share code, notes, and snippets.

@alchimere
Last active September 9, 2019 20:10
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 alchimere/323a4803981e48fed2ce86ac5027693a to your computer and use it in GitHub Desktop.
Save alchimere/323a4803981e48fed2ce86ac5027693a to your computer and use it in GitHub Desktop.
Benchmark jsonapi-render patch

$ git checkout master

$ bundle exec ruby test_jsonapi.rb

Warming up --------------------------------------
              10_000     1.000  i/100ms
Calculating -------------------------------------
              10_000      9.795  (±10.2%) i/s -     49.000  in   5.023501s

$ git checkout optimize-resource-processor

$ bundle exec ruby test_jsonapi.rb

Warming up --------------------------------------
              10_000     1.000  i/100ms
Calculating -------------------------------------
              10_000     12.998  (± 0.0%) i/s -     65.000  in   5.006506s

12.998 / 9.795 = 1.327003573251659

--> ~30% speedup

require 'benchmark'
require 'benchmark/ips'
require 'jsonapi/renderer'
# Mock class to isolate benchmark
class Test < Hash
def jsonapi_type
:test
end
def jsonapi_id
self.object_id
end
def jsonapi_related(included_relationships)
[]
end
def as_jsonapi(options = {})
{ id: jsonapi_id, type: jsonapi_type, attributes: { foo: 'bar' } }
end
end
data = []; 10_000.times { |o| data << Test.new }; data.size
Benchmark.ips do |x|
x.report('10_000') do
JSONAPI.render(data: data)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment