Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - performance.now is not a function

I have a Node.js app. When I run node -v from the command-line, I see the following:

v10.3.0

This is relevant because I'm interested in using the Performance Hooks. I've created the most basic thing I can think of, which looks like this in an in a file named 'index.js':

const performance = require('perf_hooks');

let p = performance.now();

When I run node index.js from the command-line, I get an error that says:

TypeError: performance.now is not a function

Why am I getting this error? What am I missing?

like image 294
Some User Avatar asked Jun 01 '18 15:06

Some User


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.

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.

Is performance now faster than date now?

It's purely dependent on the time since the code started running, and clock changes do not affect the time. It's also more accurate: counting us (microseconds) instead of ms. As for support, Date. now() has slightly more support than performance.


1 Answers

The perf_hooks module exports several things, one of them is performance, so using object destructuring you could do:

const { performance } = require('perf_hooks');

Or with object access:

const performance = require('perf_hooks').performance;
like image 67
J. Pichardo Avatar answered Sep 25 '22 03:09

J. Pichardo