JS Built-ins
Modulesadvanced

Lazy Loading with Dynamic Import

Dynamic import() loads modules on demand: const module = await import('./heavy');. This reduces initial bundle size. In the sandbox, simulate lazy loading with a factory function that defers computation.

πŸ’‘ Hints (5)
  • β€’ Use a closure to maintain the cached state
  • β€’ Check if cached is null before calling the factory
  • β€’ Call factory() only once, on first access
  • β€’ Store the result in cached
  • β€’ Return the cached value on subsequent calls