Promise内存泄漏

2022-9-1 前端

# 概述

Promise抛出错误如果不进行catch会造成内存泄漏

# 示例

在vue项目中使用以下代码在chrome中使用Performance Monitor查看效果如下蓝色为Js heap size,绿色为DOM Nodes

    // 捕获错误
    for (let index = 0; index < 500000; index++) {
        this.array.push({
            date: new Date()
        });
    }

    new Promise((res, rej) => {
        throw new Error();
    }).catch(e => {})
1
2
3
4
5
6
7
8
9
10

捕获错误

    // 不捕获错误
    for (let index = 0; index < 500000; index++) {
        this.array.push({
            date: new Date()
        });
    }

    new Promise((res, rej) => {
        throw new Error();
    })
1
2
3
4
5
6
7
8
9
10

不捕获错误