Ngrx大量数据导致应用速度变慢

我有一个应用程序可以加载带有元数据的某些图像。加载到内存后,单个文件夹可能会很大(〜100-142Mb)。以前,我们使用普通的旧javascript对象来管理应用程序的状态,并且一切正常,但是我想从ngrx的状态管理中受益。

我发现了ngrx,它在状态管理方面似乎是一个更明智的选择。但是,当我将这些项目添加到状态时,在将图像添加到商店时应用程序会挂起,然后在访问商店中的各个(和不相关的)标志(即UI标志-绘图已打开)时性能会降低。

1)这里的“目录”是Map ()对象,该对象保存在商店中(〜100-120Mb)。目录是具有许多嵌套值的复杂对象。加载图片并将其添加到商店后,它会:a)挂起,然后b)其他所有操作(即更改ui标志)都会变慢。

    return {
        ...state,loadedDirectories: directories,filesLoading: false,};

2)然后可以从商店中访问目录。

this.store
    .pipe(select(fromReducer.getLoadedDirectories))
    .subscribe(loadedDirectories => {
        this._directoryData = loadedDirectories;
    });

选择器看起来像这样。...

export interface ImageLoaderState {
    loadedDirectories: Map<string,Directory>;
    filesLoading: boolean;
    errorMessage: string;
}


 export class AppState {
        imageLoader: fromImageLoader.ImageLoaderState;
    }

export const combinedReducers = {
    imageLoader: fromImageLoader.imageLoaderReducer
    .... More reducers here ....
    }

// Select Image loader state.
export const selectImageLoaderState = (state: AppState) => state.imageLoader;

export const getLoadedDirectories = createSelector(
    selectImageLoaderState,(state: fromImageLoader.ImageLoaderState) => state.loadedDirectories
);

使用angular 8和以下版本的ngrx。

    "@ngrx/effects": "^8.4.0","@ngrx/store": "^8.4.0","@ngrx/store-devtools": "^8.4.0",

有没有更好的做法?即将每张图片一次添加到商店中?

maguanying412 回答:Ngrx大量数据导致应用速度变慢

ngrx存储用于应用程序状态,不如文档存储。

请参阅。

https://github.com/btroncone/ngrx-store-localstorage/issues/39

,

我看到的一个问题是如何创建新状态。您提到创建新状态时,请执行以下

    return {
        ...state,loadedDirectories: directories,filesLoading: false,};

我认为您正在创建具有大量键值对的对象,然后在再次设置loadedDirectories属性时重新创建该对象。我不确定在非常大的对象中使用传播运算符的性能成本。我建议您集中精力创建一次此属性。这可能对您有帮助

Does spread operator affect performance?

本文链接:https://www.f2er.com/3154477.html

大家都在问