Convert a partial to method/block for speed

后端 未结 6 576
暗喜
暗喜 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:32

    This is an interesting proposition and it seems harder than I thought to find information about it. However from what I can tell from the implementation of the response object I would think it should be possible to append (<<) your output to the stream object of the response object. This will be rendered as a string into the response#body as necessary.

    The tricky part is to find a good place to define your my_partial method. While it is not commonplace to do this in a View it should still be possible and I think you should have access to the response object directly. Otherwise you might define it in a helper, though you probably do not want to use this code in a controller.

    Sorry if this is rather an idea than an answer, but I lack the time to test it properly.

提交回复
热议问题