Slow json rendering

Hi,

I have around 2000 objects to render. Unfortunately the view rendering
with jbuilder takes very long time, although the objects are small like
this:

center: {lat: 48.8130685274225, lon: 10.0405494495457}
lat: 48.8130685274225
lon: 10.0405494495457
n: "Aalen/Hirtenteich"
st: null
sy: null

The result query I pass to the view looks like this
@resorts = Resort.where(:id => resort_ids).includes(:snow_reports)

A resort has many snow_reports. st and sy are fields from snow reports.

the view looks like this:

   json.array!(@resorts) do |resort|
  json.cache!("resort_light_#{resort.id}") do

    #json.partial! 'json_partials/snow_in_resort', resort: resort
    json.n resort.name
    json.sy resort.try(:snow_reports).last.try(:snow_valley) if
resort.snow_reports
    json.st resort.try(:snow_reports).last.try(:snow_summit) if
resort.snow_reports
    json.center do
        json.lat resort.centroid.lat
        json.lon resort.centroid.lon
    end
  end
end

  Rendered resorts/find.json.jbuilder (1649.0ms)
Completed 200 OK in 1889ms (Views: 1593.1ms | ActiveRecord: 77.8ms |
Solr: 0.0ms)
In development mode the request takes 5s in chrome to run.

The first thing I ve done to speed up is changing the json gem to

      gem 'oj'
      gem 'oj_mimic_json'

It speeds up slightly but not enough. I also tried active model
serialization wich was about the same time, so I prefer to stick to
jbuilder since it is convenient to assemble json objects.
So how can I speed up rendering?

Help is highly appreciated since I am stuck for a week now.

have you tried #as_json?

Hi,

I have around 2000 objects to render. Unfortunately the view rendering

with jbuilder takes very long time, although the objects are small like

this:

center: {lat: 48.8130685274225, lon: 10.0405494495457}

lat: 48.8130685274225

lon: 10.0405494495457

n: “Aalen/Hirtenteich”

st: null

sy: null

The result query I pass to the view looks like this

@resorts = Resort.where(:id => resort_ids).includes(:snow_reports)

A resort has many snow_reports. st and sy are fields from snow reports.

the view looks like this:

json.array!(@resorts) do |resort|

json.cache!(“resort_light_#{resort.id}”) do

#json.partial! 'json_partials/snow_in_resort', resort: resort

json.n [resort.name](http://resort.name)

[json.sy](http://json.sy) resort.try(:snow_reports).last.try(:snow_valley) if

resort.snow_reports

How many snow reports does a typical resort have? I ask because you’re loading all of them with the includes above, but only using the last one. If you’ve got 2000 resorts and each one has 100 reports or so, that means instantiating 200000 SnowReport objects, and that is not going to be fast.

–Matt Jones