Inline template performance #1014
-
I was curious if inline templates would be faster than using an ERB file, but after running some benchmarks I was surprised to find it is actually much slower. Here are the results for the tests I ran:
This is the code I used for the inline example: class InlineComponent < ViewComponent::Base
def initialize(name:)
@name = name
end
def call
content_tag :h1 do
"hello #{@name}"
end
end
end This makes me confident that sticking with template files is the best approach, but I was curious if anyone knows why this is slower? I'll continue to look into it myself, but if anyone already knows the cause I'd be interested to find out more. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@andrewjtait thank you for filing this issue! I wonder if this is a speed issue with ViewComponent or with Would you be up for running a similar benchmark on Rails itself, comparing I do think we should consider documenting this issue and perhaps landing an update to our internal benchmark script to include this example. Would you be up for opening a PR? |
Beta Was this translation helpful? Give feedback.
-
wow, this is helpful, I was in the process of converting an app that used a sort of custom home grown similar-to ViewComponent, to actual ViewComponent... and I have a lot of inline components, and I was starting to wonder why I wasn't getting the speed gains I was expecting. Indeed, they all use content_tag. This is somewhat distressing; I wonder why content_tag is so slow, or if there is a good alternative to it for an inline component... I guess it looks like trying to use it in it's two-arg form instead of with block param helps a lot, although of course is a lot less readadable. How odd. I wonder if it's worth trying to profile it in Rails and PR a perf improvement. |
Beta Was this translation helpful? Give feedback.
@andrewjtait thank you for filing this issue!
I wonder if this is a speed issue with ViewComponent or with
content_tag
. In my limited experience and based on some heresay, I believe thatcontent_tag
isn't the most performant way of building HTML.Would you be up for running a similar benchmark on Rails itself, comparing
content_tag
and partials?I do think we should consider documenting this issue and perhaps landing an update to our internal benchmark script to include this example. Would you be up for opening a PR?