Lazy loading (also known as asynchronous loading) is a technique used in computer programming, especially web design and web development, to defer initialization of an object 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 pages. 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. [1]
Prior to being established as a web standard, web frameworks 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 (SEO). [2]
Below is an example of lazy loading being used in Angular, programmed in TypeScript from Farata Systems [3]
@NgModule({imports:[BrowserModule,RouterModule.forRoot([{path:'',component:HomeComponent},{path:'product',component:ProductDetailComponent},{path:'luxury',loadChildren:()=>import('./luxury.module').then(m=>m.LuxuryModule),data:{preloadme:true}}]// , {preloadingStrategy: CustomPreloadingStrategy})],declarations:[AppComponent,HomeComponent,ProductDetailComponent],providers:[{provide:LocationStrategy,useClass:HashLocationStrategy},CustomPreloadingStrategy],bootstrap:[AppComponent]})
Since 2020, major web browsers have enabled native handling of lazy loading by default. [4] [5]
This allows lazy loading to be incorporated into a webpage by adding HTML attributes.
The loading
attribute support two values, lazy
and eager
. [6] 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.
<!-- These resources will be loaded immediately --><imgsrc="header_image.jpg"><imgsrc="header_image2.jpg"loading="eager"><!-- While these resources will be lazy loaded --><imgsrc="article_image.jpg"alt="..."loading="lazy"><iframesrc="video-player.html"title="..."loading="lazy"></iframe>
There are four common ways of implementing the lazy load design pattern: lazy initialization; a virtual proxy; a ghost, and a value holder. [7] Each has its own advantages and disadvantages.
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:
privateint_myWidgetID;privateWidget_myWidget=null;publicWidgetMyWidget{get{if(_myWidget==null){_myWidget=Widget.Load(_myWidgetID);}return_myWidget;}}
Or alternatively, with the null-coalescing assignment operator ??=
privateint_myWidgetID;privateWidget_myWidget=null;publicWidgetMyWidget{get=>_myWidget??=Widget.Load(_myWidgetID);}
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 conditions.
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.
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){// ...}
A value holder is a generic object that handles the lazy loading behavior, and appears in place of the object's data fields:
privateValueHolder<Widget>valueHolder;publicWidgetMyWidget=>valueHolder.GetValue();
In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed and which also avoids repeated evaluations.
Java Platform, Standard Edition is a computing platform for development and deployment of portable code for desktop and server environments. Java SE was formerly known as Java 2 Platform, Standard Edition (J2SE).
In software engineering, double-checked locking is a software design pattern used to reduce the overhead of acquiring a lock by testing the locking criterion before acquiring the lock. Locking occurs only if the locking criterion check indicates that locking is required.
An HTML element is a type of HTML document component, one of several types of HTML nodes. The first used version of HTML was written by Tim Berners-Lee in 1993 and there have since been many versions of HTML. The current de facto standard is governed by the industry group WHATWG and is known as the HTML Living Standard.
Inline linking is the use of a linked object, often an image, on one site by a web page belonging to a second site. One site is said to have an inline link to the other site where the object is located.
PyQt is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. PyQt is free software developed by the British firm Riverbank Computing. It is available under similar terms to Qt versions older than 4.5; this means a variety of licenses including GNU General Public License (GPL) and commercial license, but not the GNU Lesser General Public License (LGPL). PyQt supports Microsoft Windows as well as various kinds of UNIX, including Linux and MacOS.
In the context of a web browser, a frame is a part of a web page or browser window which displays content independent of its container, with the ability to load content independently. The HTML or media elements in a frame may come from a web site distinct from the site providing the enclosing content. This practice, known as framing, is today often regarded as a violation of same-origin policy.
In computing, a mouseover, mouse hover or hover box is a graphical control element that is activated when the user moves or hovers the pointer over a trigger area, usually with a mouse, but also possible with a digital pen. Mouseover control elements are common in web browsers. For example, hovering over a hyperlink triggers the mouseover control element to display a URL on the status bar. Site designers can define their own mouseover events using JavaScript or Cascading Style Sheets.
Link prefetching allows web browsers to pre-load resources. This speeds up both the loading and rendering of web pages. Prefetching was first introduced in HTML5.
Orange is an open-source data visualization, machine learning and data mining toolkit. It features a visual programming front-end for explorative qualitative data analysis and interactive data visualization.
A dynamic-link library (DLL) is a shared library in the Microsoft Windows or OS/2 operating system.
In computer programming, the term hooking covers a range of techniques used to alter or augment the behaviour of an operating system, of applications, or of other software components by intercepting function calls or messages or events passed between software components. Code that handles such intercepted function calls, events or messages is called a hook.
Dojo Toolkit is an open-source modular JavaScript library designed to ease the rapid development of cross-platform, JavaScript/Ajax-based applications and web sites. It was started by Alex Russell, Dylan Schiemann, David Schontzler, and others in 2004 and is dual-licensed under the modified BSD license or the Academic Free License.
In computer programming, DLL injection is a technique used for running code within the address space of another process by forcing it to load a dynamic-link library. DLL injection is often used by external programs to influence the behavior of another program in a way its authors did not anticipate or intend. For example, the injected code could hook system function calls, or read the contents of password textboxes, which cannot be done the usual way. A program used to inject arbitrary code into arbitrary processes is called a DLL injector.
Dynamic loading is a mechanism by which a computer program can, at run time, load a library into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory. It is one of the 3 mechanisms by which a computer program can use some other software; the other two are static linking and dynamic linking. Unlike static linking and dynamic linking, dynamic loading allows a computer program to start up in the absence of these libraries, to discover available libraries, and to potentially gain additional functionality.
Protel stands for "Procedure Oriented Type Enforcing Language". It is a programming language created by Nortel Networks and used on telecommunications switching systems such as the DMS-100. Protel-2 is the object-oriented version of Protel.
Extensible HyperText Markup Language (XHTML) is part of the family of XML markup languages which mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated.
HTML attributes are special words used inside the opening tag to control the element's behaviour. HTML attributes are a modifier of a HTML element type. An attribute either modifies the default functionality of an element type or provides functionality to certain element types unable to function correctly without them. In HTML syntax, an attribute is added to a HTML start tag.
Yesod is a web framework based on the programming language Haskell for productive development of type-safe, representational state transfer (REST) model based, high performance web applications, developed by Michael Snoyman, et al. It is free and open-source software released under an MIT License.
ASP.NET Web Forms is a web application framework and one of several programming models supported by the Microsoft ASP.NET technology. Web Forms applications can be written in any programming language which supports the Common Language Runtime, such as C# or Visual Basic. The main building blocks of Web Forms pages are server controls, which are reusable components responsible for rendering HTML markup and responding to events. A technique called view state is used to persist the state of server controls between normally stateless HTTP requests.