[ACCEPTED]-What is Lazy Loading?-lazy-loading
It's called lazy loading because, like a 20 lazy person, you are putting off doing something 19 you don't want to. The opposite is Eager 18 Loading, where you load something right 17 away, long before you need it.
If you are 16 curious why people might use lazy loading, consider 15 an application that takes a LOOOOONG time 14 to start. This application is probably 13 doing a lot of eager loading... loading 12 things from disk, and doing calculations 11 and whatnot long before it is ever needed.
Compare 10 this to lazy loading, the application would 9 start much faster, but then the first time 8 you need to do something that requires some 7 long running load, there may be a slight 6 pause while it is loaded for the first time. Thus, with 5 lazy loading, you are amortizing the load 4 time throughout the course of running your 3 application... and you may actually save 2 from loading things that the user may never 1 intend to use.
Lazy Loading is a programming practice in 5 which you only load or initialize an object 4 when you first need it. This can potentially 3 give you a big performance boost, especially 2 if you have a lot of components in your 1 application.
As usual, Wikipedia has more details.
Lazy loading is a concept where we delay 13 the loading of the object unit the point 12 where we need it. Putting in simple words 11 on demand object loading rather than loading 10 the objects unnecessarily. For instance 9 if you have a "Customer" class 8 which has "Orders" object aggregated. So 7 you like to load the customer data but the 6 orders objects you would like to delay until 5 your application needs it.
Below is a youtube 4 video which demonstrates how to use lazy 3 loading , how we can implement lazy loading 2 and advantages and disadvantages of the 1 same.
wikipedia's Definition Lazy loading is 3 a design pattern commonly used in computer 2 programming to defer initialization of an 1 object until the point at which it is needed. ...
The term lazy loading is usually used when 24 talking about object relational mappers. If 23 you use ADO.NET directly you always get 22 eager loading (ie it always loads just what 21 you specify).
OR-mappers like nHibernate 20 support returning proxy objects that get 19 "filled in" with the right data only when 18 you access the data. That way you only load 17 the data that you really use. This is a 16 usefull feature when you specify a lot of 15 relations between objects that can get loaded 14 from the database, you don't want the OR-mapper 13 to load all the related objects and the 12 objects related to the related objects and 11 so on. That can result in your whole database 10 getting loaded.
This problem can be prevented 9 by carefull design of your object model 8 too. (using aggregates and only loading 7 aggregate roots like in domain driven design 6 is a way to get around this without using 5 lazy loading).
Lazy loading can result in 4 the or mapper doing lots of small database 3 accesses instead of retrieving all the data 2 you need once. This can result in performance 1 problems too.
Here's an example from some actual Python 16 code I wrote:
class Item(Model):
...
@property
def total(self):
if not hasattr(self, "_total"):
self._total = self.quantity \
+ sum(bi.quantity for bi in self.borroweditem_set.all())
return self._total
Basically, I have an Item class 15 which represents an item in our inventory. The 14 total number of items we have is the number 13 that we own plus the sum of all of the items 12 that we're borrowing from various sources. These 11 numbers are all stored in our database, and 10 it would be pointless to calculate this 9 until the total is actually requested (since 8 often Items will be used without the total 7 being requested).
So the total property checks 6 whether the _total field exists. If it 5 doesn't, then the property code queries 4 the database and computes it, then stores 3 the value in the _total field so that it 2 need not be recomputed the next time it's 1 requested.
Lazy loading: you don't waste your time 14 (nor your memory) with stuff you might not 13 need. Then when you need it, it takes longer, but 12 that's fine.
Example from life: instead 11 of actually learning that French phrasebook, you 10 learn the phrases one at a time, as they're 9 needed. When does this make sense? If you're 8 only going to be in France for a short time 7 (i.e., you won't need a lot of the phrases) or 6 if you need to leave very soon. If you're 5 there for two years and/or you have a long 4 time to study, then it might be much more 3 efficient to just learn the whole phrasebook 2 up front (eager loading).
[Inspired by the 1 Atom as taught in gang terms by Venus on WKRP.]
Lazy loading is a term frequently used in 10 databases to refer to the concept of loading 9 parts of the required info only when it's 8 needed.
I.e. suppose you needed to have a 7 record which has a join of several tables. If 6 you fetched it all at once it would take 5 longer than if you would fetch say only 4 the main table. Using lazy-loading the rest 3 of the information will be fetched only 2 if it is needed. So it is actually 'efficient-loading' in 1 certain scenarios.
The other types of 'loading' is:
- Eager Loading - Loading all the connected tables at once.
is a Design pattern.
Lazy loading: Untill 8 your code require some operation done by 7 a particular object, object is not initilaized, and 6 once it's initialized it doesn't re-initialize 5 the object but uses the previously initialized 4 object.
This makes your code much more efficient 3 and helps managing memory usage.
Example 2 Applications of Lazy loading:
Ghost Lazy 1 initialization Value holder
Some of the advantages of lazy loading:
- Minimizes start up time of the application.
- Application consumes less memory because of on-demand loading.
- Unnecessary request to server is avoided.
0
An example of Lazy Loading would be a grid 12 or table with lots of data on a webpage 11 to view where the application only loads 10 what the users browser viewpoint size is 9 at that time. When they scroll down to want 8 to view more content or data, more data 7 would be loaded into view at that moment.
This 6 is becoming more of a common visual/interaction 5 design pattern as well via ajax or jQuery.
And 4 as mentioned above, opposite would be Eager 3 Loading where you don't take client into 2 consideration thus potentially having a 1 performance hit.
According to geeksforgeeks, Lazy loading 4 is a software design pattern where the initialization 3 of an object occurs only when it is actually 2 needed and not before to preserve the simplicity 1 of usage and improve performance.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.