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

5

75

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?

Farriery answered 26/9, 2017 at 22:50 Comment(8)
where is performance defined? This is being done on client or server?Ryder
Are you using firefox-like browsers?Allograph
I am not defining it anywhere as I thought this is part of the standard library. Any help with how to do it? @Ryder I am on Google Chrome.Farriery
Strange.. I just made a sample program. It works for me jsbin.com/vowinaloni/edit?js,console,outputRyder
look here: #314393Moy
@Ryder Yeah that is weird :(.Farriery
@Moy I have but that same thing ain't working for me somehowFarriery
try window.performance. I'm guessing typescript is trying to limit the scope of the performance variable when it compiles.Amytal
J
144

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();
...
Johnathon answered 4/5, 2018 at 16:48 Comment(1)
Thanks, it seems that performance is global in Node 16, but requires importing a module on older versions.Streaky
O
22

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
Organization answered 24/9, 2018 at 18:5 Comment(0)
J
16

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.

Jopa answered 8/4, 2021 at 10:0 Comment(0)
W
8

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

Woodworker answered 11/7, 2020 at 16:38 Comment(0)
N
3

For me it was on pristine next.js project, right after running installation wizard and adding apollo. Updating NODE to latest version solved the problem.

You can do with with nvm use 18 or by adding a .nvmrc file to the application.

Neilla answered 7/11, 2023 at 10:14 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Fouts
Why are people adding new answers about Node to a question tagged [frontend]?!?Rectifier

© 2022 - 2024 — McMap. All rights reserved.