Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: performance is not defined when using performance.now()

I am getting an error ReferenceError: performance is not defined when trying to use performance.now() to measure the execution time of a function call:

export async function find(someId: string, ctx: context.IContext) {        try {         var t0 = performance.now();          var res = someModel.find(someId, ctx.cookies);          var t1 = performance.now();         console.log("Call to find took " + (t1 - t0) + " milliseconds.");          return res;       } catch (err) {         console.error(err);         throw err;       }     } 

Any ideas how I can fix this?

like image 880
Elsban Avatar asked Sep 26 '17 22:09

Elsban


People also ask

What does performance now () return?

The performance. now() method returns a DOMHighResTimeStamp , measured in milliseconds. The returned value represents the time elapsed since the time origin.

What is window performance now ()?

performance. now() is a measurement of floating point milliseconds since that particular page started to load (the performance. timing. [navigationStart](https://www.w3.org/TR/navigation-timing/#dom-performancetiming-navigationstart) timeStamp to be specific).

Can I use performance now?

performance. now() has full support in all modern browsers, starting from Chrome 24, Firefox 15, and IE10.

How do I reset my performance now?

There's no way to reset performance. now() . What you can do is to save current performance. now() when the timer starts and just substract it afterwards.


1 Answers

I know this is tagged front-end but if anyone comes across this looking for a node.js solution (like me), you need to first require performance from the perf_hooks module (available in node 8.5+).

const {performance} = require('perf_hooks'); const t0 = performance.now(); ... 
like image 199
gilmatic Avatar answered Sep 21 '22 18:09

gilmatic