Now()在 nodejs 中等效吗?

我觉得问题很简单。

我正在寻找类似于 nodejs V8引擎中的 window.Performance. now ()的东西。

现在我只是用:-

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

但是,我读到 window.Performance.now ()比使用日期精确得多,因为定义了 给你

58252 次浏览

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()

What about?

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

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"

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());

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());

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());

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

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

compare solutions with and without loop.

Note down, which makes a difference performance wise ?

Try it out in JS snippets in developer tools or any JS editor.

function sum(n) {
let total = 0;
for (let i = 0; i <= n; i++) {
total += i;
}
return total;
}


var t1 = performance.now();


sum(100000000);


var t2 = performance.now();


console.log(`time elapsed: ${(t2-t1)/1000} seconds.`);


function addupto(n) {
return n * (n + 1) / 2;
}


var t3 = performance.now();


addupto(100000000);


var t4 = performance.now();


console.log(`time elapsed: ${(t4-t3)/1000} seconds.`);