From f1b18ba4120966f9c57faa474b523a36c961fc3b Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 11 Jun 2018 17:37:09 +0800 Subject: [PATCH] process: implement process.hrtime.bigint() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/21256 Reviewed-By: Gus Caplan Reviewed-By: Michaƫl Zasso Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Anatoli Papirovski --- doc/api/process.md | 32 +++++++++++++++++++++ lib/internal/bootstrap/node.js | 5 ++-- lib/internal/process.js | 10 ++++++- src/bootstrapper.cc | 1 + src/node_internals.h | 1 + src/node_process.cc | 11 +++++++ test/parallel/test-process-hrtime-bigint.js | 14 +++++++++ 7 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-process-hrtime-bigint.js diff --git a/doc/api/process.md b/doc/api/process.md index 80b70ae1151370..df544e7f0f17b9 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1163,6 +1163,9 @@ added: v0.7.6 * `time` {integer[]} The result of a previous call to `process.hrtime()` * Returns: {integer[]} +This is the legacy version of [`process.hrtime.bigint()`][] +before `bigint` was introduced in JavaScript. + 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. @@ -1191,6 +1194,33 @@ setTimeout(() => { }, 1000); ``` +## process.hrtime.bigint() + + +* Returns: {bigint} + +The `bigint` version of the [`process.hrtime()`][] method returning the +current high-resolution real time in a `bigint`. + +Unlike [`process.hrtime()`][], it does not support an additional `time` +argument since the difference can just be computed directly +by subtraction of the two `bigint`s. + +```js +const start = process.hrtime.bigint(); +// 191051479007711n + +setTimeout(() => { + const end = process.hrtime.bigint(); + // 191052633396993n + + console.log(`Benchmark took ${end - start} nanoseconds`); + // Benchmark took 1154389282 nanoseconds +}, 1000); +``` + ## process.initgroups(user, extraGroup)