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

Add option to lazy load factory definitions #522

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

Conversation

quadule
Copy link

@quadule quadule commented Jan 7, 2025

Loading factory definitions can sometimes have a noticeable impact on Rails boot time: For example, in a large app I work on FactoryBot.find_definitions takes an average of 0.3s to load 274 factories. An option to lazy load them would avoid this overhead when factories are not needed.

The new lazy_load_definitions option is disabled by default for compatibility reasons, but all tests pass for both factory_bot and factory_bot_rails when enabled. The railtie will also continue to load all factory definitions at boot if config.eager_load is enabled in the application.

FactoryBot.find_definitions
if app.config.factory_bot.lazy_load_definitions && !app.config.eager_load
require "factory_bot_rails/lazy_registry_find"
FactoryBot::Registry.prepend FactoryBotRails::LazyRegistryFind
Copy link
Author

Choose a reason for hiding this comment

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

This should probably be implemented in the factory_bot gem instead, so I'll open another PR for that if there's any interest here.

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.

1 participant