有很多字段的类/接口对性能有何影响? [英] What is the performance impact for a class / interface with a lot of fields?

查看:208
本文介绍了有很多字段的类/接口对性能有何影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的angular 4应用程序中有一个类(界面),其中有很多字段. 请注意,该类/接口的实例为immutable(即,成员将永远不会更改).

I have a class (interface) in my angular 4 app which has a lot of fields. Note that the instance of this class/interface is immutable (i.e. the members will NEVER be changed).

例如

public interface IHaveALotOfFields {
    field1: string;
    //...
    field500: string;
}

此接口是通过(提供的单件/应用程序级别)服务提供的,该服务将类公开为成员.例如

This interface is provided via a (singleton / application level provided) service which exposes the class as a member. E.g.

@Injectable()
public class MyService {
    public translations: ITranslationsProvider;
}

该服务被注入到许多组件(几乎所有组件)中,并经常用在其相应的模板中,并且经常也在组件的ts部分中使用.例如.

The service is injected into a lot of components (almost all components) and often used in their corresponding template and often also in the ts-part of the component. E.g.

@Component({
               template: `Value: {{service.field500}}`
           })
export class MyComponent {
    public constructor(public service: MyService) {
    }

    private doSomething(): string {
        return this.service.field1;
    }
}

现在我的问题:

  • 大类(具有很多字段)是否会因为检测到变化而使角度变慢?
  • 有什么方法可以将一个类标记为在检测到更改时忽略我"? (类似于ChangeDetectionStrategy.OnPush,但可以在类本身或服务的成员上声明而不是为每个组件指定此名称)
  • Will a big class (with a lot of fields) make angular slow because of the change detection?
  • Is there any way to mark a class as "Ignore me on change detection"? (something similar to ChangeDetectionStrategy.OnPush, but instead of specifying this for each component, can be declared on the class itself or on the member of the service)

请注意,我不想将所有组件的变更检测策略更改为OnPush.

Please note that I don't want to change the change-detection strategy of all my components to OnPush.

推荐答案

一个大类(具有很多字段)会由于以下原因而使角度变慢 变更检测?

Will a big class (with a lot of fields) make angular slow because of the change detection?

不.角度变化检测执行两项读取类属性的操作:

No. Angular change detection performs two operations that read class properties:

  • 在当前组件上进行DOM更新
  • 子组件/指令的输入绑定更新

对于这些操作,Angular编译器创建了两个函数:

For these operations Angular compiler creates two functions:

  • updateRenderer-读取模板中使用的字段
  • updateDirectives-读取输入绑定表达式中指定的字段

这些功能仅从服务中读取特定属性.以您为例

These functions read only specific properties from the service. For your example

Value: {{service.field500}}

updateRenderer函数将看起来大致如下:

the updateRenderer function will look something along the lines:

function(_ck,_v) {
    var _co = _v.component;
    var currVal_0 = _co.service.field500;
    _ck(_v,1,0,currVal_0);

在每个摘要循环上调用这些函数.但是如您所见,只有相关的属性将从服务中读取.因此,服务上有多少个属性都没有关系.

These functions are called on each digest loop. But as you can see only the relevant property will be read from the service. So it doesn't matter how many properties there are on the service.

是否可以将某个类标记为在检测到更改时忽略我"?

Is there any way to mark a class as "Ignore me on change detection"?

我假设您正在像在AngularJS中那样询问一次性绑定.也许它也会被添加到Angular中.如果出现问题,我将进行监视并更新答案.但是您可能知道,可以使用cd.detach/cd.attach禁用/启用组件的更改检测.您可以收听该服务的某些信号,并在需要时手动运行cd.

I assume you're asking about one-time binding like we had in AngularJS. Maybe it will be added in Angular as well. I'll be monitoring that and update the answer if something comes up. But as you probably know you can disable/enable change detection for the component using cd.detach/cd.attach. You can listen for some signal from the service and run cd manually when neeed.

以下是您可以阅读以更好地了解变更检测机制的文章列表:

Here is the list of articles you could read to understand the mechanics of change detection better:

  • Everything you need to know about change detection in Angular
  • Angular’s $digest is reborn in the newer version of Angular
  • The mechanics of DOM updates in Angular
  • The mechanics of property bindings update in Angular

这篇关于有很多字段的类/接口对性能有何影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆