Skip to content

Optimize _is_collection? method #2

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

Merged
merged 1 commit into from
May 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,12 @@ def _scope
end

def _is_collection?(object)
_object_respond_to?(object, :map, :count) && !(::Struct === object)
object.respond_to?(:map) && object.respond_to?(:count) && !(::Struct === object)
end

def _blank?(value=@attributes)
BLANK == value
end

def _object_respond_to?(object, *methods)
methods.all?{ |m| object.respond_to?(m) }
end
Comment on lines -366 to -368
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, you see a lot of this sort of arg splatting throughout jbuilder, which really adds up in terms of memory allocations. Some are necessary to support things like json.foo @foo, :id, :name, :etc, but others like this can be optimized away.

end

require 'jbuilder/railtie' if defined?(Rails)