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

[0.9 stable] Avoid Object.constants.include? #2466

Merged
merged 1 commit into from
Apr 10, 2024

Conversation

casperisfine
Copy link

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.

cc @bf4

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.
@casperisfine casperisfine changed the title Avoid Object.constants.include? [0.9 stable] Avoid Object.constants.include? Apr 10, 2024
@byroot byroot merged commit 9f7222c into rails-api:0-9-stable Apr 10, 2024
7 checks passed
@casperisfine casperisfine deleted the 0-9-stable-const-defined branch April 10, 2024 07:42
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

💯

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.

3 participants