Nodejs,调用performance.getEntriesByType时性能挂钩崩溃

gko*_*ont 7 node.js

这是我的示例代码

const _performance = require('perf_hooks').performance;

someFunction= ()=>{

    _performance.mark('mark A')

    // Do something
    const a= Array(100000)
    var boo =true;
    a.forEach(el=>{
        boo = ! boo
    })

    _performance.mark('mark B')
    _performance.measure('total','mark B','mark A');

    var measurments = _performance.getEntriesByType('measure');

    measurments.forEach(measurment =>{
        console.log(measurment.name + ' ' + measurment.duration)
    } )

}

someFunction();
Run Code Online (Sandbox Code Playgroud)

它崩溃并显示错误消息

类型错误:_performance.getEntriesByType 不是函数

知道为什么会这样吗?我在 Windows 10 上使用 node v10.7.0

sse*_*lla 12

从最新的perf_hooksAPI来看,该getEntriesByType()函数是PerformanceObserverEntryList. 试试这个:

const { performance, PerformanceObserver} = require('perf_hooks')

const obs = new PerformanceObserver((items) => {

    items.getEntries().forEach((item) => {
        console.log(item.name, + ' ' + item.duration)
    })
})
obs.observe({entryTypes: ['measure']})


function someFunction() {
    // Do something
    const a = Array(100000)
    var boo =true;
    a.forEach(el?{
        boo = ! boo

    })
}

performance.mark('mark A')
someFunction();
performance.mark('mark B')
performance.measure('total','mark A','mark B');
Run Code Online (Sandbox Code Playgroud)