Only initialize configuration-dependent features, systems, or resources when the relevant settings, flags, or conditions are actually present or enabled. This prevents unnecessary resource allocation, avoids unwanted side effects like creating default contexts, and improves performance by deferring expensive operations until they're needed.
Only initialize configuration-dependent features, systems, or resources when the relevant settings, flags, or conditions are actually present or enabled. This prevents unnecessary resource allocation, avoids unwanted side effects like creating default contexts, and improves performance by deferring expensive operations until they’re needed.
Key patterns to follow:
Example of proper conditional initialization:
// Bad: Always initializes prefs regardless of need
if (auto* browser_context = GetDefaultBrowserContext())
prefs_ = browser_context->prefs();
// Good: Only initialize when actually needed
if (gin_helper::Dictionary restore_options;
options.Get(options::kWindowStateRestoreOptions, &restore_options)) {
// Only get prefs when stateId is present
restore_options.Get(options::kStateId, &window_state_id_);
if (!window_state_id_.empty()) {
prefs_ = GetApplicationPrefs();
}
}
Example with feature flags:
// Wrap feature-specific code with build flags
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
if (offscreen_use_shared_texture_) {
dict.Set("texture", tex);
}
#endif
This approach prevents creating unnecessary contexts, avoids performance overhead, and ensures features are only configured when they will actually be used.
Enter the URL of a public GitHub repository