Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Return early from collection partial rendering if blank. #560

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tylerjc
Copy link

@tylerjc tylerjc commented Mar 31, 2024

I was messing around with making a project's Jbuilder renders more efficient the other day.

I noticed that even empty collections trigger partial renders and take milliseconds for each, although they end up just returning []. For the examples below (pulled from the README):

json.array! @posts, partial: 'posts/post', as: :post

# or
json.partial! 'posts/post', collection: @posts, as: :post

# or
json.partial! partial: 'posts/post', collection: @posts, as: :post

# or
json.comments @post.comments, partial: 'comments/comment', as: :comment

Originally, I had written a little helper method in our project to optionally include the { partial:, as: } hash to the json.comments(@post.comments) call, but that seemed cumbersome to include everywhere and I wondered why this wasn't somehow handled in Jbuilder code.

I just started messing around locally and found that this seems to take care of it, but figured I'd open a discussion and see what others think, since I'm extremely knew to Jbuilder and could be missing something obvious.

Screenshots (Console Output Differences)

  • Before PR changes (same collection):

image

  • After PR changes (same collection):

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant