简体   繁体   English

*ngFor 哪种方法更好:ng-container 或 pipe slice?

[英]Which approach for *ngFor is better: ng-container or pipe slice?

I want to show full list or only its 3 elements - conditionally ( more=true/false ).我想显示完整列表或仅显示其 3 个元素 - 有条件地( more=true/false )。 I can use pipe approach like this我可以使用这样的管道方法

<div class="table">
  <div *ngFor="let item of list|slice:0:(more ? undefined : 3 )" class="row">
      {{ item.id }} {{ item.name }}
  <div>
</div>

or ng-container approach like this或者像这样的ng-container方法

<div class="table">
  <ng-container *ngFor="let item of list; index as i">
    <div *ngIf="i<3 || more" class="row">
      {{ item.id }} {{ item.name }}
    </div>
  <ng-container>
</div>

But I don't know: which approach is better in terms of performance?但我不知道:哪种方法在性能方面更好?

UPDATE: More about this case here: How to 'show more' rows using *ngFor更新:有关此案例的更多信息: How to 'show more' rows using *ngFor

  1. The first one (pipe) will only call the slice method once and iterate through the template depending on the items that will be displayed ( 3 or all ),第一个(管道)将只调用一次slice 方法并根据将显示的项目( 3 或全部)遍历模板,
  2. while the second one (guard) will always iterate through all elements in the array and evaluate every *ngIf .而第二个(守卫)将始终遍历数组中的所有元素并评估每个*ngIf

I think the first one would be faster in terms of performance.我认为第一个在性能方面会更快。 Although, there's going to be somewhere within 10ms of a gap that isn't really noticeable by the user.尽管如此,在 10 毫秒内会有一个用户不会真正注意到的间隙。

Maybe that would be clear once the logic or the list to render scales in size.一旦逻辑或列表按大小进行缩放,这可能就很清楚了。

You could always perform a Performance Audit to compare which one takes more time rendering.您始终可以执行性能审计来比较哪一个需要更多的渲染时间。

This is what the slice approach gave me:这就是切片方法给我的:

在此处输入图片说明

And the ng-container gave me this: ng-container给了我这个:

在此处输入图片说明

But then, as I said, the difference of just 7ms won't be noticeable.但是,正如我所说,只有 7 毫秒的差异不会很明显。 But then again, there's no harm in going with the more performant approach.但话又说回来,采用更高性能的方法并没有什么坏处。

Here's a Working Demo that you can use to compare both of them.这是一个工作演示,您可以使用它来比较两者。

SlicePipe is an impure pipe. SlicePipe是一种不纯的管道。 That means it runs every time change detection runs on your component.这意味着每次在您的组件上运行更改检测时它都会运行。

The pipe creates a new array every time it's called, so it could result in excessive garbage collection if change detection is running frequently on your component.管道在每次调用时都会创建一个新数组,因此如果更改检测在您的组件上频繁运行,则可能会导致过多的垃圾回收。

You can see the effect by creating your own pipe and adding a log statement to it:您可以通过创建自己的管道并向其添加日志语句来查看效果:

@Pipe({name: 'myslice', pure: false})
export class MySlicePipe extends SlicePipe {
  transform(value: any, start: number, end?: number): any {
    console.log('Calling MySlicePipe...');
    return super.transform(value, start, end);
  }
}

Try triggering a (click) handler on your component while using the myslice pipe.尝试在使用myslice管道时在您的组件上触发(click)处理程序。 You'll see Calling MySlicePipe repeatedly in the console, indicating a new array being created each time.您将在控制台中看到重复Calling MySlicePipe ,表示每次都创建一个新数组。 Demo here .演示在这里

So, in terms of performance, I'd say the NgIf option is preferable.因此,就性能而言,我认为NgIf选项更可取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM