Skip to content

Commit

Permalink
Avoid Object.constants.include?
Browse files Browse the repository at this point in the history
This is an extremely inneficient way to check if a constant is defined.

`Object.constants.size` is `135` in a raw irb session.

On our large app, not even eager loaded it's `4521`. I didn't bother to
check in production, but it's probably several times that.

So here we're both allocating a very large array, and doing a `O(n)`
search into it.
  • Loading branch information
byroot committed Apr 10, 2024
1 parent 6d2f653 commit 573e417
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def serializer_for(resource, options = {})
if resource.respond_to?(:serializer_class)
resource.serializer_class
elsif resource.respond_to?(:to_ary)
if Object.constants.include?(:ArraySerializer)
if defined?(::ArraySerializer)
::ArraySerializer
else
ArraySerializer
Expand Down

0 comments on commit 573e417

Please sign in to comment.