Skip to content

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.

@neilvcarvalho neilvcarvalho self-requested a review January 27, 2025 17:00
@quadule quadule force-pushed the lazy-load-definitions branch from f18edf4 to b909731 Compare March 11, 2025 00:45
@quadule quadule requested a review from DoodlingDev as a code owner March 11, 2025 00:45
@quadule quadule force-pushed the lazy-load-definitions branch from b909731 to 3c76e51 Compare March 11, 2025 00:57
@neilvcarvalho neilvcarvalho requested review from vburzynski and removed request for smaboshe, sarahraqueld and minaslater August 8, 2025 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant