Giter Club home page Giter Club logo

Comments (5)

tytskyi avatar tytskyi commented on May 21, 2024 2

@mgechev there is my first iteration on trackBy topic.
I also would like to add persistence advantage of using tracking function. The idea is simple: given that rendered collection of video players, each one has the "like" button. User starts to play one video and in the middle of the process clicks "like". If an immutable collection is used (which is true in most cases, as it's suggested practice) then without trackBy DOM tree re-renders again to alter collection state. Which causes that player is stopped because it is totally new player has been rendered on its place.

But i am afraid describing this in good words and complete example will dramatically increase paragraph length which makes it less chance to be finished by the reader.

Do you think it worth adding it?

Draft

trackBy option for *ngFor directive

The *ngFor directive is used for rendering a collection.
By default *ngFor doesn't know how to identify each item of the given collection.

When the collection is changed in some way then the whole DOM tree is destroyed and recreated again. This is acceptable if the collection is totally different. But most times we have just a slightly modified collection (e.g. collection is sorted or one item is added/removed/changed). Anyway DOM tree is destroyed and recreated from the scratch again. This is unnecessary and, what even more important, may lead to poor performance of the whole app, particularly on a big collection of data.

To modify this behavior it is up to developer to provide custom tracking function as trackBy option for the *ngFor directive. Tracking function takes two arguments: index and item. Angular uses the value returned from tracking function to track items identity.

Example

@Component({
  selector: 'yt-feed',
  template: `
  <h1>Your video feed</h1>
  <yt-player *ngFor="let video of feed; trackBy: trackById" [video]="video"></yt-player>
`
})
export class YtFeedComponent {
  feed = [
    {
      id: 3849, // note "id" field, we refer to it in "trackById" function
      title: "Angular in 60 minutes",
      url: "http://youtube.com/ng2-in-60-min",
      likes: "29345"    
    },
    // ...
  ];

  trackById(index, item) {
    return item.id;
  }
}

from angular-performance-checklist.

mgechev avatar mgechev commented on May 21, 2024

Sure, if you want you can draft the suggestions here and later move them to the list?

Thanks for the suggesting the improvements!

from angular-performance-checklist.

mgechev avatar mgechev commented on May 21, 2024

Sorry for the delay.

Yes, I agree that this example may get a bit too long. The draft of trackBy sounds good to me! Maybe you can provide an external reference of the advantages of trackBy in case of immutable data?

Do you want to open a PR with the suggestion?

from angular-performance-checklist.

tytskyi avatar tytskyi commented on May 21, 2024

Yes i will open PR in the middle of the week (i did not get much free time these weekends).

Could you please clarify what do you mean under

an external reference of the advantages of trackBy in case of immutable data

?

from angular-performance-checklist.

mgechev avatar mgechev commented on May 21, 2024

I also would like to add persistence advantage of using tracking function. The idea is simple: given that rendered collection of video players, each one has the "like" button. User starts to play one video and in the middle of the process clicks "like". If an immutable collection is used (which is true in most cases, as it's suggested practice) then without trackBy DOM tree re-renders again to alter collection state. Which causes that player is stopped because it is totally new player has been rendered on its place.

I think by itself, this deserves a short blog post. I think it's worth mentioning something like:

Introducing a trackBy function will not only improve performance but also help us determine the unchanged entries in immutable data structures. For further reference take a look here.

from angular-performance-checklist.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.