68

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?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Elsban
  • 1,187
  • 2
  • 13
  • 24

5 Answers5

139

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();
...
nnov
  • 489
  • 6
  • 14
gilmatic
  • 1,764
  • 1
  • 14
  • 16
  • 3
    Thanks, it seems that `performance` is global in Node 16, but requires importing a module on older versions. – jcubic Nov 18 '21 at 16:07
20

Since the 'perf_hook' module exports multiple constructs (objects, functions, constants, etc.), you will need to explicitly specify which construct you wish to use. In this case, you need the performance construct. The declaration can be done in two ways:

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

or

const { performance } = require('perf_hooks'); //object destructuring
Lae Kettavong
  • 379
  • 2
  • 3
14

You will lose type information when using require, so the best way to import "performance" with TypeScript is using an import statement:

import {performance, PerformanceObserver} from 'perf_hooks';

const observer = new PerformanceObserver(items => items.getEntries().forEach((entry) => console.log(entry)));    
observer.observe({entryTypes: ['measure']});

performance.mark('start');
performance.mark('stop');
performance.measure('Measurement', 'start', 'stop');

Also make sure that you declared @types/node in your "dependencies" of "package.json".

Tested with TypeScript 4 & Node.js 14.

Benny Code
  • 51,456
  • 28
  • 233
  • 198
7

yes! like above answers you need to add this..

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

but you can run performance.now() inside your browser console or in your browser -> source tab -> snippet without adding above code.

you can read this to get to know more about this..

https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_now

HeshanHH
  • 653
  • 7
  • 9
1

      try {
        var t0 =  Date.now()

        var res = someModel.find(someId, ctx.cookies);

        var t1 = Date.now()
        console.log("Call to find took " + (t1 - t0) + " milliseconds.");

        return res;
      } catch (err) {
        console.error(err);
        throw err;
      }
    }
  • 1
    Would you mind adding an explanation of what is different in your code and how it solves the issue? Why does OP get the error message but you don't? – Just a nice guy Mar 17 '22 at 12:12