diff --git a/benchmark/process/bench-hrtime.js b/benchmark/process/bench-hrtime.js index 661dff43b0103c..8a2920a238d042 100644 --- a/benchmark/process/bench-hrtime.js +++ b/benchmark/process/bench-hrtime.js @@ -1,18 +1,32 @@ 'use strict'; const common = require('../common'); +const assert = require('assert'); const bench = common.createBenchmark(main, { - n: [1e6] + n: [1e6], + type: ['raw', 'diff'] }); - function main(conf) { - const n = conf.n >>> 0; + const n = conf.n | 0; + const hrtime = process.hrtime; + var noDead = hrtime(); + var i; - bench.start(); - for (var i = 0; i < n; i++) { - process.hrtime(); + if (conf.type === 'raw') { + bench.start(); + for (i = 0; i < n; i++) { + noDead = hrtime(); + } + bench.end(n); + } else { + bench.start(); + for (i = 0; i < n; i++) { + noDead = hrtime(noDead); + } + bench.end(n); } - bench.end(n); + + assert.ok(Array.isArray(noDead)); } diff --git a/doc/api/process.md b/doc/api/process.md index 4e20399d41fade..cd16dec54062f8 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1016,18 +1016,25 @@ Android) added: v0.7.6 --> -The `process.hrtime()` method returns the current high-resolution real time in a -`[seconds, nanoseconds]` tuple Array. `time` is an optional parameter that must -be the result of a previous `process.hrtime()` call (and therefore, a real time -in a `[seconds, nanoseconds]` tuple Array containing a previous time) to diff -with the current time. These times are relative to an arbitrary time in the -past, and not related to the time of day and therefore not subject to clock -drift. The primary use is for measuring performance between intervals. +* `time` {Array} The result of a previous call to `process.hrtime()` +* Returns: {Array} + +The `process.hrtime()` method returns the current high-resolution real time +in a `[seconds, nanoseconds]` tuple Array, where `nanoseconds` is the +remaining part of the real time that can't be represented in second precision. -Passing in the result of a previous call to `process.hrtime()` is useful for -calculating an amount of time passed between calls: +`time` is an optional parameter that must be the result of a previous +`process.hrtime()` call to diff with the current time. If the parameter +passed in is not a tuple Array, a `TypeError` will be thrown. Passing in a +user-defined array instead of the result of a previous call to +`process.hrtime()` will lead to undefined behavior. + +These times are relative to an arbitrary time in the +past, and not related to the time of day and therefore not subject to clock +drift. The primary use is for measuring performance between intervals: ```js +const NS_PER_SEC = 1e9; var time = process.hrtime(); // [ 1800216, 25 ] @@ -1035,14 +1042,11 @@ setTimeout(() => { var diff = process.hrtime(time); // [ 1, 552 ] - console.log(`Benchmark took ${diff[0] * 1e9 + diff[1]} nanoseconds`); + console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`); // benchmark took 1000000527 nanoseconds }, 1000); ``` -Constructing an array by some method other than calling `process.hrtime()` and -passing the result to process.hrtime() will result in undefined behavior. - ## process.initgroups(user, extra_group)