Lazy Loading
   HOME

TheInfoList



OR:

Lazy loading (also known as asynchronous loading) is a technique used in
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, especially
web design Web design encompasses many different skills and disciplines in the production and maintenance of websites. The different areas of web design include web graphic design; user interface design (UI design); authoring, including standardised code a ...
and
web development Web development is the work involved in developing a website for the Internet (World Wide Web) or an intranet (a private network). Web development can range from developing a simple single static page of plain text to complex web applications, ...
, to defer initialization of an
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an a ...
until it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used. This makes it ideal in use cases where network content is accessed and initialization times are to be kept at a minimum, such as in the case of
web page A web page (or webpage) is a World Wide Web, Web document that is accessed in a web browser. A website typically consists of many web pages hyperlink, linked together under a common domain name. The term "web page" is therefore a metaphor of pap ...
s. For example, deferring loading of images on a web page until they are needed for viewing can make the initial display of the web page faster. The opposite of lazy loading is eager loading.


Examples


With web frameworks

Prior to being established as a web standard,
web framework A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build a ...
s were generally used to implement lazy loading. One of these is Angular. Since lazy loading decreases bandwidth and subsequently server resources, it is a strong contender to implement in a website, especially in order to improve user retention by having less delay when loading the page, which may also improve
search engine optimization Search engine optimization (SEO) is the process of improving the quality and quantity of Web traffic, website traffic to a website or a web page from web search engine, search engines. SEO targets unpaid search traffic (usually referred to as ...
(SEO). Below is an example of lazy loading being used in Angular, programmed in
TypeScript TypeScript (abbreviated as TS) is a high-level programming language that adds static typing with optional type annotations to JavaScript. It is designed for developing large applications and transpiles to JavaScript. It is developed by Micr ...
fro
Farata Systems
@NgModule()


As a web standard

Since 2020, major web browsers have enabled native handling of lazy loading by default. This allows lazy loading to be incorporated into a webpage by adding
HTML attributes HTML attributes are special words used to adjust the behavior or display of an ''HTML element''. An attribute either modifies the default functionality of an element type or provides functionality to certain element types unable to function corre ...
. The loading attribute support two values, lazy and eager. Setting the value to lazy will fetch the resource only when it is required (such as when an image scrolls into view when a user scrolls down), while setting it to eager, the default state, the resource will be immediately loaded. ...


Methods

There are four common ways of implementing the lazy load design pattern: ''lazy initialization''; a ''virtual proxy''; a ''ghost'', and a ''value holder''. Each has its own advantages and disadvantages.


Lazy initialization

With lazy initialization, the object is first set to null. Whenever the object is requested, the object is checked, and if it is null, the object is then immediately created and returned. For example, lazy loading for a widget can be implemented in the C# programming language as such: private int _myWidgetID; private Widget _myWidget = null; public Widget MyWidget Or alternatively, with the null-coalescing assignment operator ??= private int _myWidgetID; private Widget _myWidget = null; public Widget MyWidget This method is the simplest to implement, although if null is a legitimate return value, it may be necessary to use a placeholder object to signal that it has not been initialized. If this method is used in a multithreaded application, synchronization must be used to avoid
race condition A race condition or race hazard is the condition of an electronics, software, or other system where the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events, leading to unexpected or inconsistent ...
s.


Virtual proxy

A virtual proxy is an object with the same interface as the real object. The first time one of its methods is called it loads the real object and then delegates.


Ghost

A ghost is the object that is to be loaded in a partial state. It may initially only contain the object's identifier, but it loads its own data the first time one of its properties is accessed. For example, consider that a user is about to request content via an online form. At the time of creation, the only information available is that content will be accessed, but the specific action and content is unknown. An example in PHP: $userData = array ( "UID" = > uniqid(), "requestTime" => microtime(true), "dataType" => "", "request" => "" ); if (isset($_POST data' && $userData)


Value holder

A ''value holder'' is a generic object that handles the lazy loading behavior, and appears in place of the object's data fields: private ValueHolder valueHolder; public Widget MyWidget => valueHolder.GetValue();


See also

*
Demand paging In computer operating systems, demand paging (as opposed to anticipatory paging) is a method of virtual memory management. In a system that uses demand paging, the operating system copies a disk page into physical memory only when an attempt is m ...
* Dynamic loading * Proxy pattern *
Lazy evaluation In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an Expression (computer science), expression until its value is needed (non-strict evaluation) and which avoids repeated eva ...
* Lazy initialization *
Software design pattern In software engineering, a software design pattern or design pattern is a general, reusable solution to a commonly occurring problem in many contexts in software design. A design pattern is not a rigid structure to be transplanted directly into s ...


References


External links


Lazy Loading, Mozilla Developer Network



JS Lazy Loading
Software design patterns Articles with example PHP code {{compu-prog-stub