Lazy Loading vs Eager Loading - Coded Java

Latest

Quality & Simple Java Content

Sep 14, 2017

Lazy Loading vs Eager Loading

Lazy Loading vs Eager Loading 

Lazy Loading vs Eager Loading

Lazy Loading

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used. 


Simply, lazy loading is the concept of loading only the requested entity or the module initially for the sake of performance with a reference to the related entities. Whenever a related entity is refereed, it is populated or loaded. This can be really contributing to the efficiency of the program.

This pattern is commonly used with most of the software applications and frameworks like Spring and Hibernate.

For an example, an application may be displaying the available user list of the application. However in the initial setup, only a list usernames are adequate. The other user profile details for the each user are required only if those details are viewed by clicking the username. Then the relevant data is loaded on demand.

This is very efficient approach rather loading all the user profile data initially as it increase the application loading time as well as the memory usage of application. 



Eager Loading

As oppose to the lazy loading, eager loading loads all the related entities of the requested entity along with the requested entity. It has some plus points as well, like reducing backed calls and higher responsiveness as the data is already loaded.



Best Approach

It is not possible to highlight one method as the better approach in general. The best approach for a specific scenario should be carefully selected considering the scenario and the use cases. For a specific scenario with the given requirements, one approach can be more suitable beyond other.

Further Reading

References

No comments:

Post a Comment