Home  >  

Advantages of Lazy Loading

Author photo
AddThis Social Bookmark Button

Wikipedia defines Lazy Loading as "a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed." Typically, we do this to make an RIA application more responsive–since we're not pulling huge amounts of data from the back end at one time, the user doesn't have to wait as long for the call(s) to complete. In particular, this pattern is especially useful with hierarchical data sets (grandparent, parent, child, etc.) where the number of nesting levels might not be known at design time.

However, there are several other advantages as well:

Necessity

If your client-side code is AS3, then if the server side code can't give you the whole tree at once, trying to load all of the data at once (i.e., at startup), doesn't work. Some languages will let you load the base level, then loop through each object and load its children, etc. In Actionscript, calls to server-side logic are asynchronous, which means that you make your call in one part of the logic, and then you have no control over when the results come back. Instead, the results will be available when the component making the call generates an event that says they are ready.

On one level, this is good–it allows the program to remain responsive while data calls are being made. However, it pretty much forces you to spread the loading of data among different functions in your code. Since you have to do this anyway, it starts to make more sense to load the data "just in time," rather than all at once.

Clarity

If all of the data loading is done on the server side, which is the main way you'd get a single "data dump" at startup of your Application, there's a lot going on that will not show up in the code. For instance, you might get a tree of hierarchical XML that is in a format that is obvious to the original developer(s), but that might be opaque to developers who are brought in later. When you can see where and how each level of object is loaded, the code can be easier to understand and debug.

Simplicity

If you're only concerning yourself with one level at a time, the code becomes a lot simpler than recursive code that tries to traverse a heirarchical structure to an unknown depth. Instead, the code can simply do one thing at a time. This is especially true if all the objects that you're instantiating are of the same basic type.

Lazy loading simplifies the code not only on the client side, but also on the server side as well. Calls for data can return much less information in a less complicated format, so the code on the server will be lighter weight and more responsive.

Hopefully, this brief overview of Lazy Loading will have piqued your interest in using it. If you'd like to see an example of Lazy Loading in action, come on over to my personal blog and check out my Lazy Loading Tree example.

Read more from Amy Blankenship. Amy Blankenship's Atom feed

Comments

4 Comments

I've never understood why it's called "lazy loading". I would think lazy loading would be the default load everything at the beginning because you're too lazy to write code to load resources as they are needed.

We should call it "smart loading" or "active loading" or something along those lines. Something that gives the act a respectful ring to it, calling it lazy isn't exactly a positive notion.

But the lingo has been around too long so we're likely stuck with it.

Joe said:

My take on the name is that 'lazy loading' is lazy because any work is being put off until its absolutely needed. Maybe 'procrastinated loading' is more accurate.

karfau said:

>Since you have to do this anyway, it starts to make more sense
>to load the data "just in time," rather than all at once.

Hm, but reading about lazy loading this argument seems counterproductive to me: If I have any difficulties with displaying something that has to be loaded in an async way, , I could be tempted to load everything needed at any time before I actually need it, just to make sure the user doesn't have to wait at nearly any click.

(I'm not doing it the way I mentioned above, I just found this one argument a but strange.)

karfau

Amy said:

When you get into the mechanics of Eager Loading in AS3 (see my this week's post http://www.insideria.com/2009/11/eager-loading-in-actionscript.html), it turns out that it can be pretty complex. There are ways to handle it, but it's sometimes easier to just give in to what As3 wants you to do.

My experience with Lazy Loading is that the amount of data you're requesting at any one time will be minimal and won't require special "tricks" on the server side that will slow down the return. This means that when the user clicks, response time is usually pretty good.

Thank you for your comment :o)

Leave a comment


Tag Cloud

Question of the Week: Dream App

If you had an unlimited budget and unlimited resources what application would you build and why would you build it?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.