是否可以在无头镀铬中使用window.performance.memory?

sno*_*bra 6 javascript google-chrome performance-testing

我想使用window.performance.memory来衡量无头chrome测试中是否有内存泄漏。

在测试中:

    beforeEach(() => {
        $('body').append(initHtml);
        console.log(window.performance.memory)
    });
Run Code Online (Sandbox Code Playgroud)

结果是:MemoryInfo {}

在开发控制台中:

console.log(window.performance.memory)
Run Code Online (Sandbox Code Playgroud)

结果是: MemoryInfo {totalJSHeapSize: 27600000, usedJSHeapSize: 16100000, jsHeapSizeLimit: 1530000000}

我的Karma.conf包含以下内容:

browsers: ['Chrome_with_memory_tests'],
customLaunchers: {
    Chrome_with_memory_tests: {
        base: 'Chrome',
        flags: ['--enable-precise-memory-info']
    }
},
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样吗?该窗口对象可用,因为我可以在其上运行其他方法。也许我需要另一个标志来启用此实验功能? https://developer.mozilla.org/zh-CN/docs/Web/API/Window/performance

Y__*_*Y__ 0

window.performance.memory的属性是不可枚举的

如果你想看到它们,你可以使用类似的东西:

console.log(JSON.stringify(window.performance.memory, ['totalJSHeapSize', 'usedJSHeapSize', 'jsHeapSizeLimit']));

// Will output something like :
// {"totalJSHeapSize":239000000,"usedJSHeapSize":225000000,"jsHeapSizeLimit":2330000000}
Run Code Online (Sandbox Code Playgroud)

您确实必须启用该enable-precise-memory-info标志,以便在无头模式下程序执行期间正确更新值。