Convert a partial to method/block for speed

后端 未结 6 569
暗喜
暗喜 2021-02-06 13:46

I have a loop that renders a partial

1000.times do |i|
  render partial: \'test\', locals: {i: i}
end

this is really slow, up to 0.1 ms for for

6条回答
  •  死守一世寂寞
    2021-02-06 14:38

    Use :collection option:

    = render partial: 'test', collection: 10.times.to_a, as: :i
    

    Docs: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

    Source: https://github.com/rails/rails/blob/master/actionview/lib/action_view/renderer/partial_renderer.rb#L284

提交回复
热议问题