---
title: Improve App Performance with the NgOptimizedImage Directive
description: Angular v14.2 includes exciting new features and improvements. In this post, learn how to implement NgOptimizedImage directive to improve app performance.
image: https://www.bitovi.com/hubfs/Improve%20App%20Performance%20with%20the%20NgOptimizedImage%20Directive.png
---

- ![AI implementation](https://www.bitovi.com/hubfs/AIConsultingIcon.svg)
  
  [AI implementation](https://www.bitovi.com/services/ai-consulting)
- ![Systems engineering](https://www.bitovi.com/hubfs/icon%20-%20backend.svg)
  
  [Systems engineering](https://www.bitovi.com/services/systems-engineering-consulting)
- ![Project Management](https://www.bitovi.com/hubfs/icon%20-%20PM.svg)
  
  [Project Management](https://www.bitovi.com/services/agile-project-management-consulting)
- ![Product Design](https://www.bitovi.com/hubfs/icon%20-%20design.svg)
  
  [Product Design](https://www.bitovi.com/services/product-design-consulting)
- ![Frontend development](https://www.bitovi.com/hubfs/icon%20-%20frontend.svg)
  
  [Frontend development](https://www.bitovi.com/services/frontend-development-consulting)
- [View more
  
  →
  
  ](https://www.bitovi.com/digital-consulting-services)

We're Experts in...

- [JavaScript](https://www.bitovi.com/services/frontend/javascript-consulting)
- [AI training](https://www.bitovi.com/ai-training-for-software-engineers)
- [Angular](https://www.bitovi.com/services/frontend/angular-consulting)
- [Design systems](https://www.bitovi.com/services/axure-figma-migration)
- [React](https://www.bitovi.com/services/frontend/react-consulting)
- [Temporal](https://www.bitovi.com/services/backend/temporal-consulting)
- [React Native](https://www.bitovi.com/services/frontend/react-consulting/react-native)
- [Node.js](https://www.bitovi.com/services/backend/nodejs-consulting)

Showcase

![Yum! Brands](https://www.bitovi.com/hubfs/yum-showcase-link-1.png)

[View case study](https://www.bitovi.com/en/bitovi-yum-case-study)

More Projects

- [![Levi's](https://www.bitovi.com/hubfs/levis.svg)](https://www.bitovi.com/web-application-consulting-work/levis-ecommerce-responsive-redesign)
- [![Christie's International Real Estate](https://www.bitovi.com/hubfs/christies.svg)](https://design.bitovi.com/christies)
- [![BAFS](https://www.bitovi.com/hubfs/bafs.svg)](https://www.bitovi.com/ux-design-consulting/ux-case-studies/bafs-ppp)
- [View more
  
  →
  
  ](https://www.bitovi.com/our-software-consulting-work)

Open Source Tools

We build powerful tools and open source them to support the community.

[See what we've built →](https://www.bitovi.com/open-source)

- [![Blog](https://www.bitovi.com/hubfs/icon%20-%20blog.svg)
  
  BlogWe post about delivering products and solving problems.
  
  ](https://www.bitovi.com/blog)
- [![Partnerships](https://www.bitovi.com/hubfs/Handshake-1.svg)
  
  PartnershipsLearn about Bitovi's technology partners
  
  ](https://www.bitovi.com/partnerships)
- [![Academy](https://www.bitovi.com/hubfs/icon%20-%20academy%20(4).svg)
  
  AcademyFree courses to build delivery skills
  
  ](https://www.bitovi.com/academy)
- [![Open source tools](https://www.bitovi.com/hubfs/icon%20-%20open%20source.svg)
  
  Open source toolsUse or contribute to our community
  
  ](https://www.bitovi.com/open-source)

Let's Connect

- [![Discord](https://www.bitovi.com/hubfs/DiscordLogo.svg)
  
  Discord
  
  ](https://discord.gg/J7ejFsZnJ4)
- [![LinkedIn](https://www.bitovi.com/hubfs/LinkedinLogo.svg)
  
  LinkedIn
  
  ](https://www.linkedin.com/company/bitovi/)
- [![GitHub](https://www.bitovi.com/hubfs/GithubLogo.svg)
  
  GitHub
  
  ](https://github.com/bitovi/)

![Eggbot](https://www.bitovi.com/hubfs/build_assets/bitovi-limbo-cms-react/338/js_client_assets/assets/eggbot-LTGhdSGL.png)

Name *

Work Email *

Phone

What's your project?

Send

### Contact Us

(312) 620-0386contact@bitovi.com

[ Angular ](https://www.bitovi.com/blog/topic/angular) |  August 30, 2022

# Improve App Performance with the NgOptimizedImage Directive

 Angular v14.2 includes exciting new features and improvements. In this post, learn how to implement NgOptimizedImage directive to improve app performance.

![Fábio Englert Moutinho](https://www.bitovi.com/hubfs/Imported%20images/T02B04353-U0293HY2Q6S-528651d5ff8c-512.jpeg)

 Fábio Englert Moutinho

Share:

[![Twitter](https://www.bitovi.com/hubfs/limbo-generated/_astro/twitter-white.os3xLc3C_Z2nW4or.svg) ](https://twitter.com/intent/tweet?text=) [![Reddit](https://www.bitovi.com/hubfs/limbo-generated/imgs/icons/reddit.png) ](http://reddit.com/submit?url=)

#### In This Series: What's New in Angular v14.2

1. Improve App Performance with the NgOptimizedImage Directive
2. [How to Use Ng Serve with Service Worker Support](https://www.bitovi.com/blog/how-to-use-ng-serve-with-service-worker-support)
3. [Developer Preview of Standalone Support in Angular Elements](https://www.bitovi.com/blog/developer-preview-of-standalone-support-in-angular-elements)

Newly released Angular 14.2 comes with a nifty image optimization feature that can be used to improve the performance of your application.

In this blog post, we will take a look at the exciting new `NgOptimizedImage` directive and explain how we used it to make this [example e-commerce application](https://e-commerce-scully-example.vercel.app/) perform up to 50% better on Lighthouse scores!

![image showing improved lighthouse score of Angular e-commerce app after implementing NgOptimizedImage](https://www.bitovi.com/hs-fs/hubfs/ngoptimizedimage-lighthousescore.png?width=694&name=ngoptimizedimage-lighthousescore.png)

## What is NgOptimizedImage?

[NgOptimizedImage](https://angular.io/api/common/NgOptimizedImage) is a Directive focused on improving Image Loading performance. As a happy side-effect, `NgOptimizedImage` enforces best practices and improves Core Web Vitals scores, which might help your application rank higher on search engines.

**Important Note:** `NgOptimizedImage` is on [Developer Preview](https://angular.io/guide/releases#developer-preview). Features on Developer Preview might change APIs without notice. If you decide to use `NgOptimizedImage` on production, be aware when updating your application.

## How to Use NgOptimizedImage

In order to use the `NgOptimizedImage` directive, identifiable by the selector `img[rawSrc]`, you need to import `NgOptimizedImage` into your Module or Standalone Component.

```
import { NgOptimizedImage } from '@angular/common';
```

### Set Up an Image Loader

Image Loaders are tools that host images and provide optimization services such as image resizing or compression.

Angular now provides popular Image Loader configurations out of the box. In our e-commerce application example, we set up [ImageKit](https://imagekit.io/) with the `provideImageKitLoader` function, whose `path` argument is the [default URL endpoint](https://imagekit.io/dashboard/url-endpoints) for an account.

```
// src/app/app.module.ts

// ... other imports
import { NgOptimizedImage, provideImageKitLoader } from '@angular/common';

@NgModule({
  declarations: [AppComponent, HomeComponent, CartComponent],
  imports: [BrowserModule, AppRoutingModule, ScullyLibModule, NgOptimizedImage],
  providers: [provideImageKitLoader('https://ik.imagekit.io/fabioemoutinho')],
  bootstrap: [AppComponent],
})
export class AppModule {}
```

### Understand NgOptimizedImage Properties

`NgOptimizedImage` adds additional properties to an `img` tag:

- `rawSrc: string`: instead of `src` full URL, `rawSrc` represents the source image name
- `rawSrcset: string`: a comma-separated [list of width or pixel density descriptors](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-srcset). Not required, but recommended. Usually used in conjunction with `sizes` attribute if you want to provide different image sources for different viewport sizes.
  
  ```
  <!-- example taken from https://angular.io/api/common/NgOptimizedImage#properties -->
  
  <img rawSrc="hello.jpg" rawSrcset="100w, 200w" />
  
  <!-- img tag above would output an img tag similar to the img below -->
  <img src="path/hello.jpg" srcset="path/hello.jpg?w=100 100w, path/hello.jpg?w=200 200w" />
  ```
- `priority: boolean`: defaults to false; If true, adds [multiple optimizations](https://developer.chrome.com/blog/angular-image-directive/#mvp-for-the-ngoptimizedimage-directive) to the image

Images are going to be lazy-loaded by default. That is, it is going to be [up to the browser to decide when to load the images](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-loading). While lazy-loading an image is usually a good approach, [in some cases](https://web.dev/priority-hints/#increase-the-priority-of-the-lcp-image), like when you want to make sure the browser will download critical images first, you might want to set the `priority` property to `true` in order to increase the performance of your app.

The `NgOptimizedImage` directive also expects every image to have explicit `height` and `width` attributes.

Here are the changes we had to make on a component’s template to use the `NgOptimizedImage` directive.

```
  <!-- src/app/home/home.component.html ‐‐>

  <picture class="picture block relative overflow-hidden">
    <img
-     [src]="product.image"
+     [rawSrc]="product.id + '.jpg'"
      [alt]="product.title"
      class="img not-loaded object-contain w-full h-full absolute top-0 left-0"
      (load)="onLoadImage($event)"
      (error)="onErrorImage($event)"
+     width="200"
+     height="200"
    />
  </picture>
```

## Performance Results

In our example application, we removed `scully` to better measure the impact of a vanilla implementation of the `NgOptimizedImage` directive on the application’s home page. We tested two different scenarios:

1. [https://e-commerce-no-scully-example-angular-13.vercel.app/](https://e-commerce-no-scully-example-angular-13.vercel.app/) - Angular 13.3.11
2. [https://e-commerce-no-scully-example.vercel.app/](https://e-commerce-no-scully-example.vercel.app/) - Angular 14.2.0, using `NgOptimizedImage`

We then ran Lighthouse locally (desktop) and had the following results:

| **Metric** | **Angular 13.3.11** | **Angular 14.2 with NgOptimizedImage** | **Impact** |
| --- | --- | --- | --- |
| First Contentful Paint | 0.4 s | 0.3 s | 25% decrease |
| Speed Index | 1.2 s | 0.6 s | 50% decrease |
| Largest Contentful Paint | 1.7 s | 1.0 s | 41% decrease |
| Time to Interactive | 0.5 s | 0.4 s | 20% decrease |
| Total Blocking Time | 0 ms | 0 ms | - |
| Cumulative Layout Shift | 0.003 | 0.003 | - |
| Performance | 93 | 99 | 6.45% increase |

Even though you get excellent performance results with the automatic lazy loading of images, `NgOptimizedImage` shines even brighter when combining the usage of image loaders with the `rawSrcset` property and `sizes` attribute. Implementing the `rawSrcset` property and `sizes` attribute enables you to further optimize your application for different devices, especially for mobile devices.

## What’s Next for Angular’s Image Directive?

Chrome’s blog post announcement about `NgOptimizedImage` directive presents an interesting [roadmap](https://developer.chrome.com/blog/angular-image-directive/#future-roadmap), and we can expect many improvements like automatic `srcset` and `sizes` attributes for responsive images, automatic `height` and `width` attributes and more.

Angular v14.2 introduces other interesting features that we will talk about next, stay tuned on [Bitovi's Twitter](https://twitter.com/bitovi).

Need help staying up to date with the latest version of Angular to take advantage of these cool new features? [Contact us](https://www.bitovi.com/frontend-javascript-consulting/angular-consulting) for Angular Consulting help!

 

[![Tag for angular upgrades](https://www.bitovi.com/hubfs/limbo/icons/tag.svg) angular upgrades ](https://www.bitovi.com/blog/topic/angular-upgrades)[![Tag for what's new in angular v14.2](https://www.bitovi.com/hubfs/limbo/icons/tag.svg) what's new in angular v14.2 ](https://www.bitovi.com/blog/topic/whats-new-in-angular-v14-2)

 Previous Post

![The 5 Tenets of Legibility in UI Design](https://www.bitovi.com/hs-fs/hubfs/The%205%20Tenets%20of%20Legibility%20in%20UI%20Design.png?height=117&name=The%205%20Tenets%20of%20Legibility%20in%20UI%20Design.png) [ The 5 Tenets of Legibility in UI Design ](https://www.bitovi.com/blog/the-5-tenets-of-legibility-of-ui-design)

  

 Next Post

![How to Use Ng Serve with Service Worker Support](https://www.bitovi.com/hs-fs/hubfs/How%20to%20Use%20Ng%20Serve%20with%20Service%20Worker%20Support.png?height=117&name=How%20to%20Use%20Ng%20Serve%20with%20Service%20Worker%20Support.png) [ How to Use Ng Serve with Service Worker Support ](https://www.bitovi.com/blog/how-to-use-ng-serve-with-service-worker-support)

```json
{
  "@context" : "http://schema.org",
  "@type" : "Organization",
  "address" : {
    "@type" : "PostalAddress",
    "addressCountry" : "United States",
    "addressLocality" : "Libertyville",
    "addressRegion" : "IL",
    "postalCode" : "60048",
    "streetAddress" : "1134 Pine Tree Lane "
  },
  "alternateName" : "Bitovi",
  "areaServed" : {
    "@type" : "GeoCircle",
    "geoMidpoint" : {
      "@type" : "GeoCoordinates",
      "latitude" : "41.8781",
      "longitude" : "87.6298"
    },
    "geoRadius" : "5000 km"
  },
  "description" : "Bitovi is a UX, UI design and front-end JavaScript development consulting company",
  "email" : "contact@bitovi.com",
  "image" : "https://www.bitovi.com/hubfs/bitovi-logo-x2.png",
  "logo" : "https://www.bitovi.com/hubfs/bitovi-logo-23-1.svg",
  "mainEntityOfPage" : {
    "@id" : "https://www.bitovi.com/blog/improve-app-performance-with-ngoptimizedimage-directive",
    "@type" : "WebPage",
    "description" : "Angular v14.2 includes exciting new features and improvements. In this post, learn how to implement NgOptimizedImage directive to improve app performance."
  },
  "naics" : "541511",
  "name" : "Bitovi Web App Consulting",
  "sameAs" : [ "https://www.facebook.com/BitoviLLC/", "https://twitter.com/bitovi", "https://www.linkedin.com/company/bitovi" ],
  "telephone" : "312-620-0386",
  "url" : "http://bitovi.com"
}
```

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Fábio Englert Moutinho"
  },
  "dateModified" : "October 6, 2023, 7:17:59 PM",
  "datePublished" : "2022-08-30 19:58:30",
  "description" : "Angular v14.2 includes exciting new features and improvements. In this post, learn how to implement NgOptimizedImage directive to improve app performance.",
  "headline" : "Improve App Performance with the NgOptimizedImage Directive",
  "image" : {
    "@type" : "ImageObject",
    "url" : "https://www.bitovi.com/hubfs/Improve%20App%20Performance%20with%20the%20NgOptimizedImage%20Directive.png"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://www.bitovi.com/hubfs/bitovi-logo-23-1.svg"
    },
    "name" : "Bitovi"
  }
}
```