Angular2 [style.width] 绑定高 CPU 使用率

Angular2 [style.width] binding high CPU usage

我目前正在为 Angular (6.0) 集成顶部栏进度指示器。

我尝试了多个 npm 包,但在我启动进度加载器后,所有包都出现了高 CPU 使用率。

Chrome 任务管理器报告 50-80% CPU 使用率。这是来自以下包的最小示例:https://github.com/aitboudad/ngx-loading-bar

模板:

<ng-container *ngIf="(loader.progress$|async) as progress">
    <div class="bar" [style.width.%]="progress">        
    </div>
</ng-container>

服务:

@Injectable()
export class LoadingBarService implements OnDestroy {
  readonly progress$ = (new Subject<number>()).pipe(debounceTime(0)) as Subject<number>;

  private _pendingRequests = 0;
  private _value = 0;
  private _incTimeout: any;

  start(initialValue = 2) {
    ++this._pendingRequests;
    if (this._value === 0 || this._pendingRequests === 1) {
      this.set(this._pendingRequests === 1 && this._value > 0 ? this._value : initialValue);
    }
  }

  set(n) {
    if (n === 0 && this._pendingRequests > 0) {
      n = 2;
    }

    this._value = n;
    this.progress$.next(n);

    if (this._pendingRequests === 0) {
      return;
    }

    clearTimeout(this._incTimeout);
    if (this._value > 0 && this._value < 100) {
      this._incTimeout = setTimeout(() => this.increment(), 250);
    }
  }
}

如您所见,该值仅每 250 毫秒更新一次。但是 CPU 使用率一直保持在 ~60-70%(一个核心)。

当我删除绑定 [style.width.%] 时,CPU 使用率下降到 5-10%。

更新样式宽度真的那么贵吗?

有没有更好(更快)的方法来更新样式的宽度?

因为在显示加载程序时使用如此高 CPU 会损害用户在导航页面时的体验。

我注意到我导入了 LoadingBarModule 两次(在主模块和组件模块中)。从组件模块中删除导入解决了这个问题。