Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs, performance hooks crash when calling performance.getEntriesByType

Tags:

node.js

This is my example code

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();

It crashes with the error message

TypeError: _performance.getEntriesByType is not a function

Any idea why is this happening? I am using node v10.7.0 on windows 10

like image 825
gkont Avatar asked Nov 09 '18 10:11

gkont


1 Answers

As of the latest perf_hooks API, the getEntriesByType() function is a method of PerformanceObserverEntryList. Try this instead:

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');
like image 95
ssemilla Avatar answered Jan 01 '23 12:01

ssemilla