Angular 4随着时间的推移变得越来越慢。

12 浏览
0 Comments

Angular 4随着时间的推移变得越来越慢。

我有一个使用Angular 4.3.5的应用程序,在使用一段时间(大约20分钟)后变得越来越慢。\n我的情况如下:\n

    \n

  • 在树莓派B 3上运行的Rest API和静态angular html/css/js
  • \n

  • 约30个树莓派B 3通过Chromium(版本58和60)访问静态angular应用程序
  • \n

\n问题出现在:\n

    \n

  • 随着时间的推移,Angular的HTTP请求变得越来越慢。例如:从约100毫秒变为约2秒
  • \n

\n其他信息:\n

    \n

  • 如果我在Chromium上按F5,Angular应用程序会恢复正常
  • \n

  • Angular使用这个模板https://themeforest.net/item/primer-angular-2-material-design-admin-template/19228165
  • \n

  • Angular使用我编写的Google Chrome/Chromium应用程序通过串口与Arduino进行通信(Chrome API:chrome.runtime.sendMessage,chrome.runtime.connect和chrome.serial)
  • \n

  • 当应用程序变慢时,客户端树莓派的资源(CPU和内存)是可用的
  • \n

  • Angular应用程序在浏览器上几乎不存储任何内容
  • \n

\n出现问题的组件如下:\n

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/takeUntil';
import { Subject } from 'rxjs/Subject';
import { SweetAlertService } from 'ng2-cli-sweetalert2';
import { ApiService } from '.././api.service';
import { NFCService } from '.././nfc.service';
@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.scss']
})
export class MenuComponent implements OnInit, OnDestroy {
  //其他代码
}

\n每次访问组件时出现延迟的方法有:\ngetCategories()\ngetSubcategories(uuid)\ngetProducts(uuid)\nconfirmDebit()\n为了测试目的,我们为每个被调用方法创建了一个新版本,这次使用promise:\ngetCategoriesP()\ngetSubcategoriesP(uuid)\ngetProductsP(uuid)\n无论调用的是哪个版本的方法,都会出现相同的问题。

0
0 Comments

Angular 4 is getting slower through time 的问题出现的原因是每次路由参数变化时都会创建一个新的 Subscription,这可能会导致许多订阅的堆积。

解决这个问题的方法是使用 switchMap,这样只会有一个订阅。具体的代码如下:

this.route.params
    .switchMap(params => {
      this.cardId = params.id;
      return this._apiService.getCardUser(params.id);
    })
    .takeUntil(this.ngUnsubscribe)
    .subscribe(
      response => {
        // SUCCESS
        this.userId = response.data[0].uuid;
        this.userBalance = response.data[0].balance;
        this.userName = response.data[0].name;
      },
      error => {
        // ERROR
        console.log('Failed ;(', error);
      }
    );

通过使用 switchMap,我们可以避免不必要的订阅堆积,从而提高 Angular 4 的性能。

0
0 Comments

Angular 4 is getting slower through time. The problem lies somewhere within the subscription mechanism in the get-Methods (getProducts, getCategories, etc.) of the api-Service.

The issue could be related to how the observable is created and handled. When calling the api-Service, the response is subscribed to. It is important to determine if the response is the original response from the http-request or if it is an observable that was created manually.

In general, it is not necessary to unsubscribe from http-calls in Angular as described in this Stack Overflow post: Do you need to unsubscribe from Angular 2 http calls to prevent memory leak?. However, if a custom observable is created instead of using the original http-observable, it may be necessary to clean it up manually.

It would be helpful to see some code from the api-Service to understand how the promises are created.

Another potential issue to consider is how the getProducts-Method is called. If a different UUID is passed every time the method is called, a new entry is written into localStorage for each UUID. This could contribute to the slowdown over time.

To address this issue, it is recommended to review the subscription mechanism in the get-Methods of the api-Service. Ensure that the original http-observable is used and not a custom observable. If custom observables are used, make sure to clean them up properly. Additionally, consider optimizing the usage of localStorage and avoid unnecessary writes to improve performance.

0
0 Comments

Angular 4的性能随着时间的推移而变慢的原因可能是因为运行应用程序作为单页应用程序(SPA)。在SPA中,每当用户访问新页面时,DOM会变得更重。因此,您需要努力保持DOM的轻量级。

以下是我发现显着提高应用程序性能的主要原因。

检查以下要点:

- 如果使用选项卡控件,则只加载活动选项卡内容,其他选项卡内容不应存在于DOM中。

- 如果要加载任何弹出窗口,请确保它在打开时仅加载主体。

- 公共组件(如确认弹出窗口和消息警报)应该只定义一次并且可以全局访问。

- 使用*ngFor和trackby来提高性能。

- 在服务完成后,调用对象的destroy方法。

在ngOnDestroy中取消订阅服务:

import { Subject } from 'rxjs/Subject'
import 'rxjs/add/operator/takeUntil';
ngOnDestroy() {        
    this.ngUnsubscribe.next(true);
    this.ngUnsubscribe.complete();
}
this.frameworkService.ExecuteDataSource().takeUntil(this.ngUnsubscribe).subscribe((data: any) => {
    console.log(data);
});

参考以下链接获取更多详细信息:

- https://medium.com/paramsingh-66174/catalysing-your-angular-4-app-performance-9211979075f6

还可以保存订阅并在ngOnDestroy中取消订阅。

0