# Is there an easy way to speed up the json rendering of 20,000+ dry-struct objects in rails?

**URL:** <https://discourse.dry-rb.org/t/is-there-an-easy-way-to-speed-up-the-json-rendering-of-20-000-dry-struct-objects-in-rails/727>\
**Category:** Uncategorized\
**Created:** [February 24, 2019, 10:56pm UTC](https://discourse.dry-rb.org/t/is-there-an-easy-way-to-speed-up-the-json-rendering-of-20-000-dry-struct-objects-in-rails/727 "2019-02-24T22:56:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![i2chris](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/i2chris/32/268_2.png) [@i2chris](https://discourse.dry-rb.org/u/i2chris)\
**Post date:** [February 24, 2019, 10:56pm UTC](https://discourse.dry-rb.org/t/is-there-an-easy-way-to-speed-up-the-json-rendering-of-20-000-dry-struct-objects-in-rails/727/1 "2019-02-24T22:56:17Z")

</div>

I’m rendering 20,000 + dry-struct objects in JSON using Rails controller. I takes about 7 seconds on Heroku to render the JSON for roughly 22 thousand dry-struct objects.  
If you know of a quicker way to generate the JSON then I’d be interested. Thanks!

```auto
Rails controller:
render json: {
                samples: sample_list.samples,
                sample_columns: sample_list.sample_columns,
                tests: sample_list.tests.index_by(&:test_id),
    }
class Limit < Dry::Struct
      attribute :type, Types::Strict::String.enum("pass_fail", "warning")
      attribute :formatted, Types::Strict::String
    end

    class LimitType < Dry::Struct
      # type can be MRL or Client Spec
      attribute :type_id, Types::Strict::Integer
      attribute :type, Types::Strict::String
      attribute :limits, Types::Array.of(::SampleTesting::ViewBuilders::Limit)

      def mrl?
        type_id == ::SampleLimits::SampleLimit::MrlLimitType
      end

      def client_spec?
        type_id == ::SampleLimits::SampleLimit::ClientSpecificationType
      end
    end

    class AResultCaveat < Dry::Struct
      attribute :id, Types::Strict::Integer
      attribute :caveat_id, Types::Strict::Integer
      attribute :text, Types::Strict::String
    end

    class Result < Dry::Struct
      transform_keys(&:to_sym)
      attribute :id, Types::Strict::Integer
      attribute :formatted_lod, Types::Strict::String
      attribute :formatted_result, Types::Strict::String
      attribute :reviewed, Types::Bool
      attribute :failed, Types::Bool
      attribute :warning, Types::Bool
      attribute :caveats, Types::Array.of(AResultCaveat).default([])
    end

    class SampleTest < Dry::Struct
      attribute :id, Types::Strict::Integer
      attribute :sample_id, Types::Strict::Integer
      attribute :test_id, Types::Strict::Integer
      attribute :results, Types::Array.of(Result)
      attribute :limit_types, Types::Array.of(LimitType).default([])
    end

    class SampleWithResults < Dry::Struct
      attribute :sample, LimsSampleReporting::ViewBuilders::Sample
      attribute :sample_tests, Types::Array.of(SampleTest)
    end

    class SampleList < Dry::Struct
      transform_keys(&:to_sym)
      attribute :samples, Types::Array.of(SampleWithResults)
      attribute :tests, Types::Array.of(Testing::Domain::Test)
      attribute :sample_columns, Types::Array.default([])
    end

```

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [February 24, 2019, 10:57pm UTC](https://discourse.dry-rb.org/t/is-there-an-easy-way-to-speed-up-the-json-rendering-of-20-000-dry-struct-objects-in-rails/727/2 "2019-02-24T22:57:38Z")

</div>

@i2chris Have you run a profiler over this? May be enlightening to see where the hotspots are.

---

<div class="post-metadata">

**Author:** ![i2chris](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/i2chris/32/268_2.png) [@i2chris](https://discourse.dry-rb.org/u/i2chris)\
**Post date:** [February 24, 2019, 10:59pm UTC](https://discourse.dry-rb.org/t/is-there-an-easy-way-to-speed-up-the-json-rendering-of-20-000-dry-struct-objects-in-rails/727/3 "2019-02-24T22:59:04Z")

</div>

I will try it again. The last time I tried it it crashed before it rendered the flamegraph.

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [February 24, 2019, 11:04pm UTC](https://discourse.dry-rb.org/t/is-there-an-easy-way-to-speed-up-the-json-rendering-of-20-000-dry-struct-objects-in-rails/727/4 "2019-02-24T23:04:00Z")

</div>

@i2chris I guess you probably want to reduce the number of records when profiling 🙂

---

<div class="post-metadata">

**Author:** ![i2chris](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/i2chris/32/268_2.png) [@i2chris](https://discourse.dry-rb.org/u/i2chris)\
**Post date:** [February 24, 2019, 11:04pm UTC](https://discourse.dry-rb.org/t/is-there-an-easy-way-to-speed-up-the-json-rendering-of-20-000-dry-struct-objects-in-rails/727/5 "2019-02-24T23:04:48Z")

</div>

Good plan 👍
