window.performance.now() equivalent in nodejs?

Javascriptnode.jsNavigation Timing-Api

Javascript Problem Overview


I think the question is straight forward.

I'm looking for something that's similar to window.performance.now() in nodejs V8 engine.

Right now I'm just using:-

var now = Date.now();
//do some processing..
console.log("time elapsed:", Date.now() - now);

But, I read that window.performance.now() is lot more accurate than using the date because of the what's defined here.

Javascript Solutions


Solution 1 - Javascript

Node v8.5.0 has added Performance Timing API, which includes the performance#now(), e.g.

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

console.log('performance', performance.now());

Solution 2 - Javascript

I would only mention that three of the reasons the author gives for the preference of the timing API in the browser wouldn't seem to apply directly to a node situation, and the fourth, the inaccuracy of Javscript time, cites an article from 2008, and I would strongly caution against relying on older material regarding Javascript performance specifics, particularly given the recent round of performance improvements all the engines have made to support "HTML5" apps.

However, in answer to your question, you should look at process.hrtime()

UPDATE: The present package (available via npm install present) provides some sugar around hrtime if you'd like it.

Note: Since the version 8.5.0 of Node, you can use performance.now()

Solution 3 - Javascript

Here's a shortcut for process.hrtime() that returns milliseconds instead of microseconds:

function clock(start) {
	if ( !start ) return process.hrtime();
	var end = process.hrtime(start);
	return Math.round((end[0]*1000) + (end[1]/1000000));
}

Usage:

var start = clock();
// do some processing that takes time
var duration = clock(start);
console.log("Took "+duration+"ms");

Will output something like "Took 200ms"

Solution 4 - Javascript

What about?

console.time('FooTimer');
// do the work
console.timeEnd('FooTimer');

Solution 5 - Javascript

process.uptime()


> "The process.uptime() method returns the number of seconds the > current Node.js process has been running. > > The return value includes fractions of a second. Use Math.floor() to > get whole seconds."

Example: Measure For Loop Execution Time


const nemo = ['nemo'];

function findNemo(array) {
  
  let start_time = process.uptime();

  for (let iteration = 0; iteration < array.length; iteration++) {
     if (array[iteration] === 'nemo') {
        console.log("Found Nemo");
     }
  }

  let end_time = process.uptime();

  console.log("For loop took this much time: ", end_time - start_time);
}

findNemo(nemo);

Example Output


enter image description here

Solution 6 - Javascript

Here's a Typescript version with process.hrtime(), based on NextLocal's answer:

class Benchmark {

    private start = process.hrtime();

    public elapsed(): number {
        const end = process.hrtime(this.start);
        return Math.round((end[0] * 1000) + (end[1] / 1000000));
    }
}

export = Benchmark;

Usage:

import Benchmark = require("./benchmark");

const benchmark = new Benchmark();

console.log(benchmark.elapsed());

Solution 7 - Javascript

To sum up and avoiding using perf_hooks

const performance = {
        now: function(start) {
            if ( !start ) return process.hrtime();
            var end = process.hrtime(start);
            return Math.round((end[0]*1000) + (end[1]/1000000));
        }
    }
console.log('performance', performance.now());

Solution 8 - Javascript

This method came into existence in version 8.5.0 of nodejs https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_measurement_apis

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshriekView Question on Stackoverflow
Solution 1 - JavascriptGajusView Answer on Stackoverflow
Solution 2 - Javascriptbarry-johnsonView Answer on Stackoverflow
Solution 3 - JavascriptjaggedsoftView Answer on Stackoverflow
Solution 4 - JavascripttenbitsView Answer on Stackoverflow
Solution 5 - JavascriptClean Code StudioView Answer on Stackoverflow
Solution 6 - JavascriptErik BarkeView Answer on Stackoverflow
Solution 7 - JavascriptloretoparisiView Answer on Stackoverflow
Solution 8 - JavascriptapenasView Answer on Stackoverflow