Skip to content

Commit 573e417

Browse files
committed
Avoid Object.constants.include?
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.
1 parent 6d2f653 commit 573e417

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/active_model/serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def serializer_for(resource, options = {})
6262
if resource.respond_to?(:serializer_class)
6363
resource.serializer_class
6464
elsif resource.respond_to?(:to_ary)
65-
if Object.constants.include?(:ArraySerializer)
65+
if defined?(::ArraySerializer)
6666
::ArraySerializer
6767
else
6868
ArraySerializer

0 commit comments

Comments
 (0)