Problem: Lazy-Load a Heavy Service Only When First Used

Medium
30 min
Delay the creation of a resource-intensive object until its first actual use.

Problem statement

Your system utilizes a heavy-duty data service, DataService, which performs an expensive initialization when it is constructed. This service is always created on startup—even if no code ends up using it—wasting time and memory.

You need to optimize performance by deferring the creation of the service until it’s truly needed. In other words, the service should only initialize when a method or property is accessed for the first time. You’ll use the Proxy Pattern to transparently handle this deferred initialization, without requiring any changes to how other modules interact with the service.

Goal

Create a function createLazyService() that returns a Proxy. The Proxy should instantiate the real service object only when a property is first accessed or a method is first called.

Constraints

  • You must use a Proxy and the get trap.

  • Do not instantiate the real service until the first access.

  • Once initialized, all subsequent accesses should delegate directly to the real object.

  • No conditional checks outside the Proxy logic.

Sample output

The examples below illustrate what the output should look like:

const service = createLazyService();
console.log('Service created but not initialized yet.');
/* Expected output:
Service created but not initialized yet.
*/
console.log(service.fetchData()); // triggers initialization
/* Expected output:
Initializing heavy DataService...
Fetched data
*/
console.log(service.fetchData()); // should reuse the same instance
/* Expected output:
Fetched data
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Lazy-Load a Heavy Service Only When First Used

Medium
30 min
Delay the creation of a resource-intensive object until its first actual use.

Problem statement

Your system utilizes a heavy-duty data service, DataService, which performs an expensive initialization when it is constructed. This service is always created on startup—even if no code ends up using it—wasting time and memory.

You need to optimize performance by deferring the creation of the service until it’s truly needed. In other words, the service should only initialize when a method or property is accessed for the first time. You’ll use the Proxy Pattern to transparently handle this deferred initialization, without requiring any changes to how other modules interact with the service.

Goal

Create a function createLazyService() that returns a Proxy. The Proxy should instantiate the real service object only when a property is first accessed or a method is first called.

Constraints

  • You must use a Proxy and the get trap.

  • Do not instantiate the real service until the first access.

  • Once initialized, all subsequent accesses should delegate directly to the real object.

  • No conditional checks outside the Proxy logic.

Sample output

The examples below illustrate what the output should look like:

const service = createLazyService();
console.log('Service created but not initialized yet.');
/* Expected output:
Service created but not initialized yet.
*/
console.log(service.fetchData()); // triggers initialization
/* Expected output:
Initializing heavy DataService...
Fetched data
*/
console.log(service.fetchData()); // should reuse the same instance
/* Expected output:
Fetched data
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
// Simulated heavy service
class DataService {
constructor() {
console.log('Initializing heavy DataService...');
}
fetchData() {
return 'Fetched data';
}
}
// Your code here
// Example usage
const service = createLazyService();
console.log('Service created but not initialized yet.');
/* Expected output:
Service created but not initialized yet.
*/
console.log(service.fetchData()); // triggers initialization
/* Expected output:
Initializing heavy DataService...
Fetched data
*/
console.log(service.fetchData()); // should reuse the same instance
/* Expected output:
Fetched data
*/