5 ways of Improving performance in JS or Angular apps

 1. debounceTime / debounce

If you have a searchbox that makes an API call, always use debounceTime (rxjs operator) or debounce.

Also use switchMap to cancel making outdated calls to API

checkout the example here: https://stackblitz.com/edit/base-angular-12-app-crzqez



2. Use trackBy when rendering Arrays in for loop


On click of add users button, users are assigned to a new array. Although the values are exactly the same, the list gets re-rendered.

Implement trackBy function

<div *ngFor="let u of users; trackBy: userTrackBy">
  {{ u.name }}
</div>
<button (click)="addUser()">add user</button>
export class TestTrackbyComponent {
  users = [
    { name: 'Leanne'id: 1 },
    { name: 'Ervin'id: 2 },
    { name: 'Patricia'id: 3 },
    { name: 'Kurtis'id: 4 },
    { name: 'Glenn'id: 5 },
    { name: 'Mandy'id: 6 },
    { name: 'Melissa'id: 7 },
  ];

  addUser() {
    this.users = [
      { name: 'Leanne'id: 1 },
      { name: 'Ervin'id: 2 },
      { name: 'Patricia'id: 3 },
      { name: 'Kurtis'id: 4 },
      { name: 'Glenn'id: 5 },
      { name: 'Mandy'id: 6 },
    ];
  }

  userTrackBy(indexanyuser: { idany }) {
    return user.id;
  }
}

Now, the list does not re-render.

Consider, we have a different ID added. Then, only the user with new ID will be rendered and not the whole list.

https://stackblitz.com/edit/stackblitz-starters-ymrtyn

3. Serve Images/fonts Assets from a CDN

Instead of making a request to the server everytime, setup a CDN so that the cached version is loaded after the 1st time, for every subsequent request thereby making the webpage faster

4. Render Blocking JavaScript

Ex: Adding script tags regularly will block HTML parsing when the script is downloaded and executed. So its always a good idea to place it at the end of body tag instead of placing it in the head tag.

In this case, they only begin to download after the entire HTML has been downloaded. However, because the download of these scripts starts later, elements loaded by them, such as ads, animations, or dynamic functionalities, might load later than the rest of the frontend, especially if it’s a longer script. This can result in noticeable delays and lagging UIs on slower connections, which is bad for the user experience.

Use async or defer instead.

Only difference between them:

async

defer

HTML parsing does not stop while downloading async script files.

Same

As soon as script file is downloaded, it pauses HTML parsing and executes the JS file

As soon as script file is downloaded, it continues to finish parsing the HTML file. Once complete, it executes the JS file

Green: HTML Parsing

Red: Script download

Yellow: script execution

5. Use Web workers for CPU intensive tasks

More about web workers here: https://javascripthisday.blogspot.com/2023/09/web-workers.html












Comments

Popular posts from this blog

Inside the JavaScript Memory Box: Visualizing Variables, References, and Copies

React & State Management: All Concepts

React: Communication between components